Results 1 to 5 of 5

Thread: sorting lists

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    120
    Reputation
    88

    sorting lists

    I have a list of strings.
    These strings are f.e. 5,8...a,k...
    When I sort then 5,8... is placed before a, k...
    Can I sort the list so that I get a,k,...,5,8..
    Thanks for any response.

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    Providing a single example of a result is useless. Please describe the actual rules you want to apply to perform the sort because it's those rules that any code will need to implement. Writing code without knowing what the code actually has to do is always a challenge. The result is not what the code has to do. The steps to get to that result is what the code has to do so if you haven't considered the steps then you shouldn't even be thinking about writing code.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    120
    Reputation
    88

    sorting lists

    I do not think that an example is useless.
    Let's take another
    cd,bac,31,bca,af,11,23,08
    Sorting this
    08,11,23,31,af,bac,bca,cd
    But I want
    af,bac,bca,cd,08,11,23,31
    I think that the problem is clear
    I see one a solution, I have to do something before because vb.net can't do it immediately
    The set of elements are from another greater and known set of 100 elements
    so I can with a select case gaves all the elements an known prefix 100 or 200
    125cd,115bac,20031,120bca,110af,20011,20023,20008
    When I sort now I get a good ranking and I must now remove the prefix
    Sorry if I was not clear in my problem.
    Thanks for your answer, see you maybe a better way?

  4. #4
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    There are always multiple ways that a specific list can be sorted to give a specific result. Many of those ways will not produce the desired result for many other lists. I don't want to guess because I don't want to guess wrong. If it's too much effort for you to describe the actual requirements then it's too much trouble for me to provide a solution that may or may not be a waste of time.

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,207
    Reputation
    2859
    If you just want standard string comparison and then move the group of strings that starts with a digit to the end then that can easily be done with Linq:
    Dim reorder = (From x In strings
    Order By x
    Group By bool = Char.IsDigit(x(0))
    Into g = Group Order By bool
    ).SelectMany(Function(y) y.g)

    This can of course also be done with a standard comparison function where you check if first char is a digit and conditionally compare x to y or reverse y to x.

    I also agree with jmcilhinney that you haven't explained the required sorting well.
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

Similar Threads

  1. Question Sorting lists and inheritance
    By edpfister in forum VB.NET General Discussion
    Replies: 9
    Last Post: 11-03-2012, 5:24 AM
  2. Sorting 2 lists based on only one of the lists.
    By NJDubois in forum VB.NET General Discussion
    Replies: 3
    Last Post: 04-02-2011, 7:22 AM
  3. Question Sorted Lists
    By Clearshot in forum VB.NET General Discussion
    Replies: 2
    Last Post: 06-16-2010, 3:20 PM
  4. Sorting two lists
    By xpertwinner in forum VB.NET General Discussion
    Replies: 1
    Last Post: 01-16-2009, 7:35 PM
  5. Sorted Lists
    By iloveorangesoda in forum VB.NET General Discussion
    Replies: 3
    Last Post: 05-24-2005, 8:42 PM

Bookmarks

Posting Permissions

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