0

given list and set of letters to sort the list using java stream

List<String> names= Arrays.asList("Robin","Zax","John");
String order = "ZALBKNDWMTFCGOIUHEPQXSYVRJ"; 

EXAMPLE: Input: List names= Arrays.asList("Robin","Zax","John"); String order = "ZJR"

Output: ["Zax","John","Robin"]

Input 2: List<String> names= Arrays.asList("Robin","Zax","John","Rohan"); String order = "OZJRHBAS";

OUTPUT: ["Zax","John","Rohan","Robin"]

 names.stream().sorted(new MyComparator(order)).collect(Collectors.toList()).forEach(System.out::println); 

I just want the implementation of compare method below is what i have tried but its sorting on the basis of only first letter. Is there any way it can be done in such a way the all the letters of string are taken care for sorting.

class MyComparator implements Comparator<String> {
    private String order;

    MyComparator(String order) {
        this.order = order;
    }

    @Override
    public int compare(String s1, String s2) {
        char[] charsOfS1 = s1.toCharArray();
        char[] charsOfS2 = s2.toCharArray();
        int proximity = 0;
        for(int i=0; i<Math.min(charsOfS1.length,charsOfS2.length);i++){
            int check = order.indexOf(s1.charAt(i)) - order.indexOf((s2.charAt(i)));
            if(check<0)
                proximity--;
            else if(check>0)
                proximity++;
        }
        return proximity;
    }
}
10
  • 2
    "I just want the implementation of compare method" - okay, so what are the actual requirements? How far have you got in implementing them? We really don't have nearly enough information to help you at the moment. Commented Nov 14, 2022 at 8:47
  • given alphabetical order using stream and comparator -> "Zax","John","Robin" or "Zax","Robin","John" !! for a solution maybe -> return o2.compareTo(o1); Commented Nov 14, 2022 at 8:57
  • just take the 1st char of each name, get their positions in the order string, and compare the 2 positions themselves Commented Nov 14, 2022 at 9:01
  • @JonSkeet I just want to make use of order given for sorting the string names in compare method, If i do return o2.compareTo(o1); it will not sort it in given order it will sort it in default alphabetical order. Commented Nov 14, 2022 at 9:07
  • 2
    Don’t post you code as comment. Edit your question. Commented Nov 14, 2022 at 15:50

1 Answer 1

1

You can simply compare the index position of the first character of strings:

public int compare(String s1, String s2) {
    return order.indexOf(s1.charAt(0)) - order.indexOf((s2.charAt(0)));
}

Full Demo:

import java.util.Comparator;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

class MyComparator implements Comparator<String> {
    private String order;

    MyComparator(String order) {
        this.order = order;
    }

    @Override
    public int compare(String s1, String s2) {
        return order.indexOf(s1.charAt(0)) - order.indexOf((s2.charAt(0)));
    }
}

public class Main {
    public static void main(String[] args) {
        // Test
        List<String> names = Arrays.asList("Robin", "Zax", "John");
        String order = "ZJR";
        System.out.println(names.stream().sorted(new MyComparator(order)).toList());
    }
}

Output:

[Zax, John, Robin]
Sign up to request clarification or add additional context in comments.

2 Comments

But if you compare only charAt(0) it will sort it on the basis of first letter for example: ` List<String> names= Arrays.asList("Robin","Zax","John","Rohan"); String order = "OZJRHBAS"; ` the OUTPUT will be Zax John Robin Rohan but here Rohan should come before Robin.
@vishal: It's almost as if you haven't provided complete requirements...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.