@@ -0,0 +1,1636 @@
+import java.util.*;%0A/*%0A* Given a defined priority list and a string, return the sorted results. %0A* priority list, %5Bp,e,a,l%5D, string, %22apple%22   should return %22ppeal%22%0A*%0A*/%0A%0Apublic class SortByPriority%7B%0A  public static String sort(char%5B%5D priority, String str)%7B%0A    if(str == null %7C%7C str.length() %3C 2)%7B%0A      return str;%0A    %7D%0A    //Create a hash map to store the key-value pair. In this case, key is the character in priority list and value is its index.%0A    Map%3CCharacter,Integer%3E map = new HashMap%3C%3E();%0A    for(int i = 0;i %3C priority.length;i++)%7B%0A      map.put(priority%5Bi%5D,i + 1);%0A    %7D%0A    //Convert the given string to a new character array.%0A    char%5B%5D array = str.toCharArray();%0A    //Store characters are not in the priority list.%0A    StringBuilder sBuilder = new StringBuilder();%0A    int%5B%5D sort = new int%5Barray.length%5D;%0A    for(int i = 0;i %3C array.length;i++)%7B%0A      if(!map.containsKey(array%5Bi%5D))%7B%0A        sBuilder.append(array%5Bi%5D); %0A      %7Delse%7B%0A        int indexOfPattern = map.get(array%5Bi%5D);%0A        sort%5Bi%5D = indexOfPattern;%0A      %7D%0A    %7D%0A    Arrays.sort(sort);%0A    char%5B%5D sorted = new char%5Barray.length%5D;%0A    int index = 0;%0A    for(int i = 0;i %3C sorted.length;i++)%7B%0A      if(sort%5Bi%5D != 0)%7B%0A        for(Map.Entry entry : map.entrySet())%7B%0A          if(entry.getValue().equals(sort%5Bi%5D))%7B%0A            sorted%5Bindex++%5D = (char)entry.getKey();%0A            break;%0A          %7D%0A        %7D  %0A      %7D%0A    %7D%0A    String res = new String(sorted);%0A    res = res + sBuilder.toString();%0A    return res;%0A  %7D%0A%0A  public static void main(String%5B%5D args)%7B%0A    char%5B%5D priority = %7B'p','e','a','l'%7D;%0A    System.out.println(sort(priority,%22apple%22));%0A  %7D%0A%7D%0A

Given a defined priority list and a string. Return a new string which contains characters sorted based on priority from priority list.