How sort a string array based on the location of a word that the first string contains in a second string array, android, java -


i trying sort string array use in list view. have 2 string arrays 1 words want display, other sort order want. words aren't in sort list @ end. example:

string[] order = {"paperclip", "pencil", "earphones", "pen"}; string[] displaylist = {"pencil 1", "pen 1", "dog 1", "earphones 1", "pen 2", "paperclip 1", "pencil 2", "pen 3", "earphones 2"}; 

the way want displaylist sorted afterward be:

string[] displaylistsorted = {"paperclip 1", "pencil 1", "pencil 2", "earphones 1", "earphones 1", "earphones 2", "pen 1", "pen 2", "pen 3", "dog 1"} 

i able keep list in same case after sort.

i have looked through lot of different tutorials , other stack questions custom comparators, beginner level knowledge of java ended forcing me write question here.

thanks help!

you iterate on set of words , compare them 1 one array of search-terms. if find match, add word list lazy created list of matches term.

if through set, join result lists in order of search terms , add remaining words @ end.

    list<string>[] results = new list[order.length];     linkedlist<string> remainders = new linkedlist<string>();      (string word : displaylist) {         boolean found = false;         for(int i=0; i<order.length; i++) {             if(word.contains(order[i])) {                 if(results[i] == null) results[i] = new linkedlist<string>();                 results[i].add(word);                 found = true;                 break;             }         }          if( !found ) {             remainders.add(word);         }     }      list<string>theresult = new arraylist<string>(displaylist.length);     ( list<string>result : results) {         if( result != null ) theresult.addall(result);     }      theresult.addall(remainders); 

Comments