bitanganguly
bitanganguly

Reputation: 41

How to sort an array of strings in a customized order?

I have an array of strings in Java which I read from an excel file where the orders will vary every time:

//This is an example of users list  
String[] usersList = ["Printers", "Configuration","Admin", "Service Desk", "Event Manager"]

I want to sort the array of strings in a customized order as:

private final String[] userSortOrder = ["Admin","Printers","Configurations","Event Manager","Service Desk"]

How can I apply the sorted order into my userList array of strings?

Upvotes: 4

Views: 622

Answers (3)

Amal K
Amal K

Reputation: 4939

Use the overload of Arrays.sort that takes an implementation of Comparator<T> as the second parameter. Implement the Comparator<String> interface to implement custom comparison.


I have used a HashMap to assign a weight/priority to each entry and then used this priority to compare. An object of higher priority will be considered greater. You can customize the priority by changing the second argument of map.put(). The way the compare function of the Comparator<T> interface works is, for every two objects, if:

  • if the int returned is negative, second element is greater
  • if the int returned is 0, both elements are equal
  • if the int returned is positive, first element is greater
class CustomComparator implements Comparator<String>
{
    HashMap<String, Integer> map;
    
    public CustomComparator()
    {
        map = new HashMap<>();
        map.put("Admin", 1); 
        map.put("Printers", 2);
        map.put("Configuration", 3);
        map.put("Event Manager", 4);
        map.put("Service Desk", 5);
    }
    
    public int compare(String s1, String s2) {
        return map.get(s1) - map.get(s2);
    }
}
class Main {
     public static void main(String []args){
        System.out.println("Hello World");
        String[] usersList = {"Printers", "Configuration","Admin", "Service Desk", "Event Manager"};
        System.out.println("Before sorting: ");
        Arrays.toString(usersList);
        Arrays.sort(usersList, new CustomComparator());
        System.out.println("After sorting: ");
        Arrays.toString(usersList);
     }  
}

Since Comparator<T> is a functional interface, you can also use a lambda expression and define the map in the same function:

HashMap<String, Integer> map = new HashMap<>();
map.put("Admin", 1);
map.put("Printers", 2);
map.put("Configuration", 3);
map.put("Event Manager", 4);
map.put("Service Desk", 5);
            
Arrays.sort(usersList, (s1, s2) -> map.get(s1) - map.get(s2));

System.out.println("After sorting:");

Arrays.toString(usersList);

Upvotes: 0

zhh
zhh

Reputation: 2406

I recommend using a Map to store the order of those Strings, then sort using a custom Comparator.

HashMap<String, Integer> orderMap = new HashMap<>();
for (int i = 0; i < userSortOrder.length; i++)
    orderMap.put(userSortOrder[i], i);
Arrays.sort(usersList, (a, b) -> orderMap.get(a) - orderMap.get(b));

Note that you must make sure every String in userList has its own order in orderMap.

Upvotes: 0

You just need to use sort method from java utils Array, giving a comparator object, with a method compare that should return: - a negative number if first object is under second one - a positive number if first object is above second one - 0 if both objects are equal , as:

Arrays.sort(sorted, new Comparator<String>() {
public int compare(String o1, String o2) {
    int result = 0;
    // Ordering algorithm here
    return result;
}});

Upvotes: 1

Related Questions