|
-
Aug 29th, 2016, 07:38 AM
#1
Thread Starter
Frenzied Member
sort list in custom order
Hi,
I was wondering if it's possible to sort a string list by custom order, ex, instead of ordering alphabetically A-Z, i want to do it using my own order first (M to Z ) then (A to M). Any tip will be appreciated
Thanks
-
Aug 29th, 2016, 07:47 AM
#2
Re: sort list in custom order
This isn't really an ASP-related question ... for better results it should be in the VB.NET sections - I'll ask the mods to move it.
That said look at the IComparable ... it allows you to implement your own brand of comparison logic for sorting.
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
-tg
-
Aug 29th, 2016, 08:03 AM
#3
Re: sort list in custom order
Thread moved to the 'VB.Net' forum (thanks tg )
-
Aug 29th, 2016, 08:34 AM
#4
Re: sort list in custom order
Sorting a list by a custom condition is simple but you have to be able to describe the condition. In your case, if you mean that you want to sort only by the first letter in that way and the rest standard alphabetically then that's not too hard but if you want to compare all letters that way then it's a bit harder, although still not too difficult.
I'm afraid that tg has given you a bit of a bum steer though. It's actually IComparer that you would need to look at, not IComparable. In fact, it would be IComparer(Of T) where T is String in your case. IComparable and IComparable(Of T) are interfaces that the items themselves implement so that they can be implicitly compared by a sorting routine. To specify a custom sort order you have to provide an IComparer or IComparer(Of T), which is an object that knows how to compare two items in the list in the way you desire. If the sort is a one-off then you might also prefer to simply use a Comparison(Of T) delegate rather than defining a whole class to implement IComparer(Of T).
-
Aug 29th, 2016, 09:34 AM
#5
Re: sort list in custom order
 Originally Posted by jmcilhinney
I'm afraid that tg has given you a bit of a bum steer though. It's actually IComparer that you would need to look at, not IComparable. In fact, it would be IComparer(Of T) where T is String in your case. IComparable and IComparable(Of T) are interfaces that the items themselves implement so that they can be implicitly compared by a sorting routine. To specify a custom sort order you have to provide an IComparer or IComparer(Of T), which is an object that knows how to compare two items in the list in the way you desire. If the sort is a one-off then you might also prefer to simply use a Comparison(Of T) delegate rather than defining a whole class to implement IComparer(Of T).
That's what I thought at first too... and had written IComparer at first... when I went to link to it, I found IComparable... and in one of the links it talked about the difference (I now wished I had linked to it too) and while they work similar, one was for sorting the other straight up compare ... it's possible I mis read it and got the two backwards though... but I'd swear I saw that IComparable is for working with lists and sorting.
-tg
-
Aug 29th, 2016, 11:38 AM
#6
Re: sort list in custom order
Well, yeah, kind of. IComparable would be what you implement for a class if you want to give it some custom sorting, but IComparer is what you would give to the list to sort standard things in a custom way. In the case of a bunch of strings, IComparable wouldn't work unless you wrapped the strings in classes that could implement IComparable. You can then just call the .Sort method of the list. However, since all you have is a standard string, then you can call the version of .Sort that takes the IComparer and pass in a means to sort those strings in a custom way.
My usual boring signature: Nothing
 
-
Aug 29th, 2016, 06:32 PM
#7
Thread Starter
Frenzied Member
Re: sort list in custom order
Thanks for the info, this looks a bit complicated than expected. I was basically trying to sort a list for a non-english language by providing the chars. I will try to look for code examples.
thanks
-
Aug 29th, 2016, 06:55 PM
#8
Re: sort list in custom order
 Originally Posted by techgnome
That's what I thought at first too... and had written IComparer at first... when I went to link to it, I found IComparable... and in one of the links it talked about the difference (I now wished I had linked to it too) and while they work similar, one was for sorting the other straight up compare ... it's possible I mis read it and got the two backwards though... but I'd swear I saw that IComparable is for working with lists and sorting.
-tg
IComparable is for working with lists and sorting but it's the items that implement it. For instance, the String class already implements IComparable and that's how you're able to sort an array or collection of Strings without providing any special instructions. IComparer is for when list items don't implement IComparable or you don't want to use the default comparison. For instance, you can simply call Sort on a List(Of String) and the items will be sorted alphabetically because that's how the IComparable implementation of the String class works. If you wanted to sort by length though, you'd have to provide an IComparer that compared by length.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|