As Adrian Larson and Mark Pond mentioned, Comparable can work equally well for any data type.
Here is an example on how to implement it. If the member variable type is not String (in your case Integer), you have to write the comparison logic instead of the default String CompareTo
method. Let's take the below example, there is Student Class having Name (String) and Age (Integer) variable. Now check the difference between two versions, where first one is using Name for comparison and Second one is for Age Comparison. Hope this helps.
Comparison based on Name
global class Student implements Comparable {
global String StudentName {get;set;}
global Integer StudentAge {get; set;}
global Student(String Name, Integer Age) {
StudentName = Name;
StudentAge = Age;
}
global Integer compareTo(Object ObjToCompare) {
return StudentName.CompareTo(((Student)ObjToCompare).StudentName);
}
}
Comparison based on Age
global class Student implements Comparable {
global String StudentName {get;set;}
global Integer StudentAge {get; set;}
global Student(String Name, Integer Age) {
StudentName = Name;
StudentAge = Age;
}
global Integer compareTo(Object ObjToCompare) {
Student compareToStud = (Student)ObjToCompare;
if (StudentAge == compareToStud.StudentAge) return 0;
if (StudentAge > compareToStud.StudentAge) return 1;
return -1;
}
}
You can find more details in the below links.
1 - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm
2 - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm