Want to sort strings in a collection array, any idea???  
Author Message
vcboy





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

Hi,
I am a beginner in C# and want to sort the strings in a collection array. Can you please give me which collection array class one will fit with my requirement
Thanks,
vcboy


Visual C#8  
 
 
Ala Kushlaf





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

The easiest way is to use generics I guess and it's very efficient and strongly typed..here is a sample code take a look at it

Add this to the namespaces

using System.Collections.Generic;

List<string> ls = new List<string>();

ls.Add("Small");

ls.Add("Large");

ls.Sort();

foreach (string s in ls)

{

MessageBox.Show(s);

}

Good Luck


 
 
James Curran





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

If you are still using .Net 1.1 (and can't use generics), you can use ArrayList, which also has a Sort() method (Oddly, neither StringCollection nor StringDictionary does)

 
 
Nimrand





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

There's also Array.Sort :-).
 
 
vcboy





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

Thanks. I got it. But the string objects have keys attached to them. So I like to sort them both string and keys as they are associated. But ArrayList has sort() function but it will only sort the string objects and leaving keys associated to the strings. Any idea, please
Thanks,
vcboy

 
 
Nimrand





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

I have an idea of what you're trying to do, but I think you're going to need to explain it in more detail. How do these string objects have keys associated with them


 
 
James Curran





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

OK, Quick & Dirty:

Create a Hashtable & an arraylist. Put the string & data into the hashtable, the string by itself into the arraylist. Sort the Arraylist. USe the arraylist to pluck data out of the hashtable:

Hashtable ht = ...
ArrayList al = ....
foreach (.....)
ht[str] = data;
al.Add(str);

al.Sort()
foreach(string str in al)
data = ht[str];

The right way:

Define a new class which holds both the string & the data. Implement IComparable. Define the CompareTo method as "return this.Name.CompareTo(obj.Name);"



 
 
vcboy





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

Ok, let's say I want to manipulate grocery list and their quantities like below:
Items quantity
Pear 29
Apple 3
Strawberry 15
Orange 8
I like to sort the items in alphabetically but their quantities will also be associated after sorting. I think it makes sense.
Thanks,
vcboy

 
 
ahsankhan





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

There is a Array.Sort function that takes a key and value array as arguments. For example:

string [] applicationNames = ...

string [] applicationIDs = ...

Array.Sort( applicationNames, applicationID, null);

Please check link http://msdn2.microsoft.com/en-us/library/2b9cyk6x.aspx


 
 
Luke D





PostPosted: Visual C# Language, Want to sort strings in a collection array, any idea??? Top

The generic Array.Sort is great.

using Pair = KeyValuePair<string, MyCustomObject>;

class Program
{

// Compares two Pairs
public static int Compare( Pair lhs, Pair rhs )
{
return lhs.Key.CompareTo( rhs.Key );
}

static void Main( string[] args )
{
Pair[] array = new Pair[10];

// ...
// Add your pairs here
// ...

// Uses the named delegate to do the comparison
System.Array.Sort( array, Compare );

// Uses anonymous delegate to do the comparison.
System.Array.Sort( array,
delegate( Pair lhs, Pair rhs ) {
return lhs.Key.CompareTo( rhs.Key );
} );
}
}


The Pair contains both your key, and your values, no class required. The key and value can be any type, here I'm using string and MyCustomObject, but they could both be custom types.

Then in the sort function, you just pass a delegate that can compare two of the key types and return an int telling if the left pair is less-than, greater-than, or equal to the right pair. I'm just forwarding to testing the left and the right keys agains each other.

Note that the delegate can be either named or anonymous, it just needs to match the Comparison<T> delegate (essentially a function of the form "int Foo<T>( T t1, T t2 )").

I like this solution the most because it requires a minimum of extra work, and is totally flexible.

Hope this helps someone.
Luke