Java: sorting an array according to matching string / pattern

I need to sort an array in which the corresponding elements appear, and others down.

For example. I have an array: [z, asxdf, abasdf, abcasdf, b, bc, bcd, c] I need it when I pass KeyWord, suppose "b" , then it should sort the given array, in which the first line starts with b. . , and rest after that. Which will generate the final result: [b, bc, bcd, z, c, .. (rest).]

If possible using a comparator in Java?

String keyWord = "b"; String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"}; Arrays.sort(s, new Comparator<String>() { @Override public int compare(String o1, String o2) { //Code to sort array according to need } }); System.out.println(Arrays.toString(s)); 

Result -> [ b , bc , bcd , z , c , ...]

(I can use List instead of Array or whatever if it helps me solve this problem)

+4
source share
7 answers

When asked if it is possible to do this using a comparator, the answer is yes: you just need to create a new Comparator class instead of creating an anonymous comparator, for example:

  class MyComparator implements Comparator<String> { private final String keyWord; MyComparator(String keyWord) { this.keyWord = keyWord; } @Override public int compare(String o1, String o2) { if(o1.startsWith(keyWord)) { return o2.startsWith(keyWord)? o1.compareTo(o2): -1; } else { return o2.startsWith(keyWord)? 1: o1.compareTo(o2); } } } 

and then use this comparator in your code:

  String keyWord = "b"; String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"}; Arrays.sort(s, new MyComparator(keyWord)); System.out.println(Arrays.toString(s)); 
+10
source

Since the string length after keyword matches seems to matter, here is my version:

 Arrays.sort(s, new Comparator<String>() { @Override public int compare(String o1, String o2) { boolean o1Has = o1.startsWith( keyWord ); boolean o2Has = o2.startsWith( keyWord ); if( o1Has && !o2Has ) return -1; else if( o2Has && !o1Has ) return 1; else if( o1Has && o2Has ) return 0; else return o1.length() - o2.length(); } }); 

Productivity:

[b, bc, bcd, z, c, asxdf, abasdf, abcasdf]

+3
source
 final String keyWord = "b"; String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"}; Arrays.sort(s, new Comparator<String>() { @Override public int compare(String o1, String o2) { boolean o1_has_keyWord = o1.indexOf(keyWord.charAt(0)) == 0 && o1.contains(keyWord); boolean o2_has_keyWord = o2.indexOf(keyWord.charAt(0)) == 0 && o2.contains(keyWord); if (o1_has_keyWord && o2_has_keyWord) { if (o1.length() == o2.length()) { if (o1.indexOf(keyWord.charAt(0)) > o2.indexOf(keyWord.charAt(0))){ return -1; } else if (o1.indexOf(keyWord.charAt(0)) == o2.indexOf(keyWord.charAt(0))){ return 0; } else { return 1; } } else if (o1.length() > o2.length()) { return 1; } else { return -1; } } else if (o1_has_keyWord && !o2_has_keyWord) { return -1; } else if (!o1_has_keyWord && o2_has_keyWord) { return 1; } return 0; //Code to sort array according to need } }); System.out.println(Arrays.toString(s)); 

Output:

 [b, bc, bcd, z, asxdf, abasdf, abcasdf, c] 
+1
source

I will also add that you can read about Comparator and Comparable in this Oracle tutorial http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html .

+1
source

What about:

 List result = new ArrayList(); for(int i=0;i<s.length();i++) { if(s[i].startsWith(keyWord)) { result.add(s[i], 0); // Adding to the beginning } else { result.add(s[i]); // Adding to the end } } 
0
source

just because it is shorter:

 @Override public int compare(String o1, String o2) { boolean firstMatches = o1.startsWith(keyWord); boolean secondMatches = o2.startsWith(keyWord); if (firstMatches != secondMatches) { return firstMatches ? -1 : 1; } return 0; // or refine the sort sort here } 
0
source

Please check the following

 package com.pantech.sam.demos; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class SortArray { /** * @param args */ private final static char keyWord = 'b'; public static void main(String[] args) { // TODO Auto-generated method stub String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"}; System.out.println("FIRST WAY " + sortArray(s)); Arrays.sort(s, new CustomeComparator(keyWord)); System.out.println("SECOND WAY" + Arrays.toString(s)); } static List<String> sortArray(String[] sArray){ Arrays.sort(sArray) ; List<String> l1 = Arrays.asList(sArray); List<String> l2 = new ArrayList<String>(l1.size()); int index = 0; for (String str : l1) { if (str.charAt(0) == keyWord) { l2.add(index++,str); // add at start }else{ l2.add(l2.size(), str); // add at end } } return l2; } } class CustomeComparator implements Comparator<String> { private final char keyWord; CustomeComparator(char keyword2) { this.keyWord = keyword2; } @Override public int compare(String o1, String o2) { // TODO Auto-generated method stub if(o1.charAt(0) == (keyWord)) { return o2.charAt(0) == (keyWord)? o1.compareTo(o2): -1; } else { return o2.charAt(0) == (keyWord)? 1: o1.compareTo(o2); } } } 
0
source

Source: https://habr.com/ru/post/1488004/


All Articles