Here is the code I have so far:
package Demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("3.3.x");
list.add("1.2.x");
list.add("2.3.x");
VersionComparator x = new VersionComparator();
Collections.sort(list, x );
for(String str : list)
System.out.println(str);
}
static class VersionComparator implements Comparator {
public int compare(String str1, String str2) {
String firstNumberStringOne = str1.split(".")[0];
String firstNumberStringTwo = str2.split(".")[0];
String secondNumberStringOne = str1.split(".")[1];
String secondNumberStringTwo = str2.split(".")[1];
return;
}
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return 0;
}
}
}
I would like to sort the items in my list by the first number that appears before the period. If those two numbers are equal then go to the second number and compare those numbers. And if equal just return them.
I am not sure how to use the comparator. I tried to implement it and the compiler complained that I needed to add in the second compare method with the override. I also do not know how to do the comparison in terms of how it is being returned. I understand that if its equal then it returns a 0 and its less than then it returns a -1 and greater than a 1. But in terms of coding I am lost as to how does the program know how to sort it.