none
Sort not in Sequence RRS feed

  • Question

  • HI All,

    Test for sort method but the position not at sequence.

    Before Sort List show as;

    4 Four
    3 Three
    1 One
    2 Two
    5 five
    7 seven
    9 nine
    6 six
    8 egiht
    10 ten
    11 elevent

    After Sort as show bellow;

    1 One
    10 ten
    11 elevent
    2 Two
    3 Three
    4 Four
    5 five
    6 six
    7 seven
    8 egiht
    9 nine

    Expected Result;

    1 One
    2 Two
    3 Three
    4 Four
    5 five
    6 six
    7 seven
    8 egiht
    9 nine
    10 ten
    11 elevent

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Dim G_ As New List(Of String)
        Dim J_ As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim H_ As New List(Of String)
            Dim I_ As New List(Of String)
    
            H_.Add("4")
            H_.Add("3")
            H_.Add("1")
            H_.Add("2")
            H_.Add("5")
            H_.Add("7")
            H_.Add("9")
            H_.Add("6")
            H_.Add("8")
            H_.Add("10")
            H_.Add("11")
    
    
            I_.Add("Four")
            I_.Add("Three")
            I_.Add("One")
            I_.Add("Two")
            I_.Add("five")
            I_.Add("seven")
            I_.Add("nine")
            I_.Add("six")
            I_.Add("egiht")
            I_.Add("ten")
            I_.Add("elevent")
    
            For I As Integer = 0 To H_.Count - 1
                TextBox1.AppendText(H_(I) & " " & I_(I) & vbCrLf)
                G_.Add(H_(I) & " " & I_(I))
            Next
    
    
        End Sub
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            G_ = G_.Select(Function(item) CStr(item)).OrderBy(Function(item) item).Select(Function(item) CStr(item)).ToList
            G_.ForEach(Sub(item) TextBox2.AppendText(item.ToString + vbCrLf))
            G_.ForEach(Sub(item) J_.Add(item.ToString + vbCrLf))
    
        End Sub
    
    End Class

    thank.

    Tuesday, May 29, 2018 4:06 AM

Answers

  • Yes of course it does as you show. You do a sort on words. If you want to do it on the preceding numbers you have to make the numbers right. 

    For instance

    08 eight
    10 ten

    Easy to do using the by you from previous question already known split with a delimiter " " and then a tostring("00")


    Success
    Cor

    Tuesday, May 29, 2018 9:20 AM
  • If we change to List H_ to integer it follow sequence but not List I_.

    If you want to sort both lists into the same sequence then create a class that includes both the string and the numeric value.  You already have an example of that.

    Then sort a collection of instances of that class, using the numeric field as the sort key.  You already have an example of that.

    • Marked as answer by mipakteh Wednesday, May 30, 2018 5:57 AM
    Tuesday, May 29, 2018 9:50 AM

All replies

  • Test for sort method but the position not at sequence.

    You are doing a string sort.  '1' comes before '2', so '10...' comes before '2 ….'. If you want the sequence to be numeric then you have to sort numbers, not strings.


    • Edited by Acamar Tuesday, May 29, 2018 4:19 AM fmt
    Tuesday, May 29, 2018 4:18 AM
  • If we change to List H_ to integer it follow sequence but not List I_.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Dim G_ As New List(Of String)
        Dim J_ As New List(Of String)
    
        Dim H_ As New List(Of Integer)
        Dim I_ As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            H_.Add(4)
            H_.Add(3)
            H_.Add(1)
            H_.Add(2)
            H_.Add(5)
            H_.Add(7)
            H_.Add(9)
            H_.Add(6)
            H_.Add(8)
            H_.Add(10)
            H_.Add(11)
    
            I_.Add("Four")
            I_.Add("Three")
            I_.Add("One")
            I_.Add("Two")
            I_.Add("five")
            I_.Add("seven")
            I_.Add("nine")
            I_.Add("six")
            I_.Add("egiht")
            I_.Add("ten")
            I_.Add("elevent")
    
            For I As Integer = 0 To H_.Count - 1
                TextBox1.AppendText(H_(I) & " " & I_(I) & vbCrLf)
                G_.Add((H_(I) & " " & I_(I) & vbCrLf))
            Next
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            G_ = G_.Select(Function(item) (item)).OrderBy(Function(item) item).Select(Function(item) (item)).ToList
            G_.ForEach(Sub(item) TextBox2.AppendText(item))
    
            'H_ = H_.Select(Function(item) (item)).OrderBy(Function(item) item).Select(Function(item) (item)).ToList
            'H_.ForEach(Sub(item) TextBox2.AppendText(item & vbCrLf))
    
            'For I As Integer = 0 To H_.Count - 1
            'TextBox3.AppendText(H_(I) & " " & I_(I) & vbCrLf)
            'Next
    
    
    
        End Sub
    End Class
    

    Tuesday, May 29, 2018 8:47 AM
  • Yes of course it does as you show. You do a sort on words. If you want to do it on the preceding numbers you have to make the numbers right. 

    For instance

    08 eight
    10 ten

    Easy to do using the by you from previous question already known split with a delimiter " " and then a tostring("00")


    Success
    Cor

    Tuesday, May 29, 2018 9:20 AM
  • If we change to List H_ to integer it follow sequence but not List I_.

    If you want to sort both lists into the same sequence then create a class that includes both the string and the numeric value.  You already have an example of that.

    Then sort a collection of instances of that class, using the numeric field as the sort key.  You already have an example of that.

    • Marked as answer by mipakteh Wednesday, May 30, 2018 5:57 AM
    Tuesday, May 29, 2018 9:50 AM
  • If we change to List H_ to integer it follow sequence but not List I_.

    If you want to sort both lists into the same sequence then create a class that includes both the string and the numeric value.  You already have an example of that.

    Then sort a collection of instances of that class, using the numeric field as the sort key.  You already have an example of that.

    Acamar, 

    Just a little addition, what you describe goes only well if the numbers are unique. What mipakteh currently shows is luckily now a readable sample. But if the true data is 1 Cor and 1 Acamar, then 1 Cor can come normally before Acamar.  


    Success
    Cor

    Tuesday, May 29, 2018 10:00 AM
  • But if the true data is 1 Cor and 1 Acamar, then 1 Cor can come normally before Acamar.

    We will need to wait and see if OP provides a specification for the task that goes to that level of detail.

    Tuesday, May 29, 2018 11:26 PM
  • Perhaps you should try a SortedDictionary<tkey, tvalue>Class</tkey, tvalue> of Integer, String where "Represents a collection of key/value pairs that are sorted on the key.".

    La vida loca

    Wednesday, May 30, 2018 12:28 AM
  • Thank Acamar,

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim H_ As New List(Of Integer)
            Dim I_ As New List(Of String)
    
            H_.Add(4)
            H_.Add(3)
            H_.Add(1)
            H_.Add(2)
            H_.Add(5)
            H_.Add(7)
            H_.Add(9)
            H_.Add(6)
            H_.Add(8)
            H_.Add(10)
            H_.Add(11)
    
    
            I_.Add("Four")
            I_.Add("Three")
            I_.Add("One")
            I_.Add("Two")
            I_.Add("five")
            I_.Add("seven")
            I_.Add("nine")
            I_.Add("six")
            I_.Add("egiht")
            I_.Add("ten")
            I_.Add("elevent")
    
    
            Dim Items As New List(Of ItemInfo)
    
            For I As Integer = 0 To H_.Count - 1
                Dim V As Integer = H_(I)
                Dim V1 As String = I_(I)
    
                Items.Add(New ItemInfo With {.H_ = V, .I_ = V1})
    
    
    
            Next
    
            Dim SortedItems As IEnumerable(Of ItemInfo) = Items.OrderBy(Function(item) item.H_)
            SortedItems.ToList.ForEach(Sub(item) TextBox3.AppendText(item.ToString + vbCrLf))
    
    
        End Sub
    
    End Class
    
    Public Class ItemInfo
        Public H_ As Integer
        Public I_ As String
    
        Public Overrides Function ToString() As String
            Return H_.ToString & " : " & I_.ToString
        End Function
    End Class
    

    Wednesday, May 30, 2018 5:57 AM