Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array sort method 1

Status
Not open for further replies.

copeZero

Programmer
Aug 31, 2007
46
0
0
CA
Hi I have an array and was wondering how to implemtn Array.Sort() to best help my situation... The array holds the record index from a table. So if i query the db, get the record set, the "riskz" object works like
riskz[0].columnName

that will return the first record from the table and the value in the columnName.

The risks[] has upwards of 13 items.

I need to sort the array somehow so that i can set variable to the columns of information where.. say.. columnName value=4

Simply put, i'm looking to determine what index in the array has columnName=4 (and there will always be only one record)

Am i explaining myself correctly? Thanks for the help.

and i need to set the values of the record for
 
Not sure I fully understand your question, but this will work for a one dimensional array.

Code:
Array.IndexOf(MyArray, MySearchValue)

If you have multiple columns, you probably should look at using a DataTable.

Hope this helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
You can just use the sort method of an array or if it's an array of classes, then implement the IComparer.

As an added note, use Generics if you can. The List<> is a great way to work with arrays. The sort feature is easy to use there too.

Off the top of my head, in order to sort from a List<> you need to create a class similar to this:

public class DateTimeSorter : IComparer<DateTime>
{
public int Compare(DateTime x, DateTime y)
{
return x.CompareTo(y); //do your comparison here
}
}


Then to use this from your List<DateTime> you would do this:

List<DateTime> alldates = new DateTime();

alldates.Sort(new DateTimeSorter());


You probably aren't using DateTime but you can compare anything inside your Compare() method above.

 
JurkMonkey, great suggestion on using linked lists. Once I learned about linked lists, I stopped using arrays for the most part.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top