0

I have a list of strings

Dim myList as new List(Of String)

1.7 
1.8 
1.9 
1.10    
1.10.1.1
1.10.1.2
1.10.1.3
1.7.1.1
1.7.1.2
1.7.1.3
1.7.1.4

and I want to order them

1.7 
1.7.1.1
1.7.1.2
1.7.1.3  
1.7.1.4
1.8 
1.9 
1.10    
1.10.1.1
1.10.1.2
1.10.1.3

using

myList.Sort( comparer here? )

how do I write an simple comparer function?

UrsulRosu
  • 507
  • 1
  • 6
  • 16
  • 2
    Check out [this question](http://stackoverflow.com/questions/8975698/implementing-custom-icomparer-with-string) to get started. – Koen Jan 17 '14 at 09:47
  • You're welcome. If you get stuck somehow, just update your question with the relevant code-snippet. – Koen Jan 17 '14 at 09:50

2 Answers2

2

To save a bit of work you could also use Version instead of String:

Dim versions As New List(Of Version) From {New Version("1.7"),
                                           New Version("1.8"),
                                           New Version("1.9"),
                                           New Version("1.10"),
                                           New Version("1.10.1.1"),
                                           New Version("1.10.1.2"),
                                           New Version("1.10.1.3"),
                                           New Version("1.7.1.1"),
                                           New Version("1.7.1.2"),
                                           New Version("1.7.1.3"),
                                           New Version("1.7.1.4")}

versions.Sort()
For Each v As Version In versions
    Console.WriteLine(v.ToString)
Next
jo0ls
  • 389
  • 1
  • 9
0
Collections.sort(myList, new Comparator<String>(){
        public int compare(String str1, String str2) {
            return str1.compareTo(str2);
        }
    });

or

Collections.sort(myList, (String str1, String str2) -> {return str1.compareTo(str2);}); //java 8
qiaoba
  • 5
  • 1
  • 3
  • 3
    It is not that helpful to reply to a VB.Net tagged questions with C# syntax unless the C# code has direct equivalence in VB. – rheitzman Jan 17 '14 at 16:42