Sorting with Punctuation -

Posted on 2014-08-08
Last Modified: 2016-02-17
I have an array list of names (Lastname, FirstName). Sort and Compare do not return the correct output.  Some of the names (first and last) have hyphens in them. Anyone know how to work around this?
Question by:mosemadl
    LVL 27

    Accepted Solution

    Comparing and sorting are two different things, if you have encountered this issue then you can Try using the sort key to get an accurate compare,

    Private Shared Function CompareStringUsingSortKey(left As String, right As String) As Integer
          Dim sk1 As SortKey = CultureInfo.InvariantCulture.CompareInfo.GetSortKey(left)
          Dim sk2 As SortKey = CultureInfo.InvariantCulture.CompareInfo.GetSortKey(right)
          Return SortKey.Compare(sk1, sk2)
    End Function

    For sorting, you can use linq
    Dim sorted = things.[Select](Function(s) s.Split("-"C)).OrderBy(Function(x) Double.Parse(x(0))).ThenBy(Function(x) Double.Parse(x(1))).[Select](Function(x) [String].Join("-", x)).ToList()

    Basically split and join
    LVL 44

    Expert Comment

    If the strings are "lastname, firstname" format, I don't see any problem sorting them.  How does the hyphen cause you problems?

    Author Closing Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now