does C# have any collection objects that support sort functionality so that I dont have to write my

  • Thread starter Thread starter Marina
  • Start date Start date
M

Marina

What kind of object is it? Sorting of strings and numerics is already
supported.

If it's some custom object you wrote, .NET has no of knowing the right way
to sort it. So you will have to do some work to define the sort.
 
does C# have any collection objects that support sort functionality so that
I dont have to write my own sorting algorithm?
 
Daniel said:
does C# have any collection objects that support sort functionality so that
I dont have to write my own sorting algorithm?

Of course. You can sort arrays, you can sort the ArrayList class, you
can sort the generic List<T>, &c.
 
If it's a custom object, all you need to do is implement the IComparable
interface on the object's class. Then you will be able to sort an Array
of the custom objects, without implementing a sorting algorithm.
 
Keep in mind, though, that the objects in the collection have to implement
the IComparable interface. That's true for both the Array and ArrayList
classes. The system data types (including strings) implement this interface.
But if you are looking to sort custom objects you created, you need to make
sure to implement it yourself.

Also, since arrays are strongly typed, you know that all elements are of the
same type and should be comparable. However, that's not necessarily the case
for ArrayLists. So you need to be careful what you store in these types of
collections. The Sort() method will utilize each object's IComparable
implementation (regardless of the type), so be sure that a meaningful
comparison is possible.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Daniel said:
does C# have any collection objects that support sort functionality so that
I dont have to write my own sorting algorithm?

System.Array.Sort on T[] and System.Collections.SortedList can be used
as a list with a small adapter.

Note that (contrary to what is stated by other posters) you do *not*
have to implement IComparable on the objects that are to be sorted, all
C# collections (and System.Array.Sort) accept IComparer instances to use
for the sorting.
 
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

Helge Jensen said:
does C# have any collection objects that support sort functionality so
that
I dont have to write my own sorting algorithm?

System.Array.Sort on T[] and System.Collections.SortedList can be used as
a list with a small adapter.

Note that (contrary to what is stated by other posters) you do *not* have
to implement IComparable on the objects that are to be sorted, all C#
collections (and System.Array.Sort) accept IComparer instances to use for
the sorting.

True, if you use one of the overloaded sort methods and specify an instance
of IComparer. However, if you just use the static Array.Sort(Array) method
or the instance method ArrayList.Sort() all collection objects need to
implement IComparable.

Sorry if my initial post wasn't quite clear on this.
--
Helge Jensen
mailto:[email protected]
sip:[email protected]
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top