Skip to content

This jar helps you be able to sort objects based on a string value inside the object

Notifications You must be signed in to change notification settings

dss-kenya/CustomComparator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

CustomComparator

This jar helps you be able to sort objects based on a string value inside the object (the string value being alphanumeric)

Problems with Comparable and String.CASE_INSENSITIVE_ORDER

  • Alphanumeric strings are not sorted correctly

Given:

  • Grade 1
  • Grade 2
  • Grade 13

The result set is:

  • Grade 1
  • Grade 13
  • Grade 2

How to use CustomComparator

  • Use Collections.sort method to sort the object list, and pass the CustomComparator Object that takes the Class being compared, the methodname used for sorting, and the Class type

For example:

public static List<Grades> getSortedList(List<Grades> gradeList, Class<?> classType, String methodName) {
		Collections.sort(gradeList, new CustomComparator<Grades>(classType, methodName));
		return gradeList;
}

Grades is the class that needs to be sorted, so we pass the list of Grades class, and the classType as Grades.class and the method name that needs to be used to sort the class

Input

  • Grade 1
  • Grade 13
  • Grade 6
  • Grade 4
  • PRE-K
  • K

Output

  • Grade 1
  • Grade 4
  • Grade 6
  • Grade 13
  • K
  • PRE-K

About

This jar helps you be able to sort objects based on a string value inside the object

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published