views:

463

answers:

5

I am using VS2008, and ASP.NET 3.5 C#

I have an array list that is populated using a textBox & button for input of numbers.

After clicking the button, I need to display the highest number in the arraylist.
So far I know I need a loop to compare the the items within the arraylist.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;

    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList basket;

            basket = (ArrayList)(Session["basket"]);

            lblPrintOut.Text = "";
            for (int x = 0; x < basket.Count; x++)
            {
               //In here i need to make sure that I 
               //compare the all the values with each other.
               //My question is HOW?
            }
         }
     }

Should I maybe use a .SORT and then somehow pick the highest number on the list? What do you guys think?

A: 

Using Linq you should be able to get the max from a list of objects.

you can sort, and pick the last/first element, or you can itterate through the list and keep a var of the max object you find in the array list.

astander
A: 

you can try this...

basket[basket.Count-1]//since the index start from 0
Muhammad Akhtar
Same timing! Code is worth a thousand words though, good good.
Wez
A: 

The basket arraylist should have a COUNT property, and remember to -1 since it's a zero-based list.

Wez
+2  A: 

If you are using .NET 3.5, the following is probably easiest. It requires that all of the items in the array list be of the same type, otherwise the .Cast extension will fail:

int maxValue = basket.Cast<int>().Max();
jrista
sweet and simple. thanks my man.
NoDinero
A: 

You should use Sort() method as it gives you flexibility (considering future modifications) and provides sorting for standard value types and strings. When your int list is sorted you can read topmost value as max value.

terR0Q