Results 1 to 8 of 8

Thread: sort list in custom order

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: sort list in custom order

    Thread moved to the 'VB.Net' forum (thanks tg )

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: sort list in custom order

    Quote Originally Posted by jmcilhinney View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  7. #7

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: sort list in custom order

    Quote Originally Posted by techgnome View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width