Java 通过Index(索引)数组同时对多个数组进行排序
时间:2022-10-08
示例代码:
int[] indexes = new int[]{0,2,8,5};
String[] sources = new String[]{"how", "are", "today", "you"};
String[] targets = new String[]{"I", "am", "thanks", "fine"};
实现效果:
indexes -> {0,2,5,8}
sources -> {"how", "are", "you", "today"}
targets -> {"I", "am", "fine", "thanks"}
根据indexes对这三个数组进行排序
1、使用比较器Comparator
import java.io.*; import java.util.*; >Test { public static String[] sortWithIndex (String[] strArr, int[] intIndex ) { if (! isSorted(intIndex)){ final List<String> stringList = Arrays.asList(strArr); Collections.sort(stringList, Comparator.comparing(s -> intIndex[stringList.indexOf(s)])); return stringList.toArray(new String[stringList.size()]); } else return strArr; } public static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i + 1] < arr[i]) { return false; }; } return true; } // Driver program to test function. public static void main(String args[]) { int[] indexes = new int[]{0,2,8,5}; String[] sources = new String[]{"how", "are", "today", "you"}; String[] targets = new String[]{"I", "am", "thanks", "fine"}; String[] sortedSources = sortWithIndex(sources,indexes); String[] sortedTargets = sortWithIndex(targets,indexes); Arrays.sort(indexes); System.out.println("Sorted Sources " + Arrays.toString(sortedSources) + " Sorted Targets " + Arrays.toString(sortedTargets) + " Sorted Indexes " + Arrays.toString(indexes)); } }
输出:
Sorted Sources [how, are, you, today] Sorted Targets [I, am, fine, thanks] Sorted Indexes [0, 2, 5, 8]
2、使用Lambda表达式
import java.io.*; import java.util.*; public >Test { public static String[] sortWithIndex (String[] strArr, int[] intIndex ) { if (! isSorted(intIndex)) { final List<String> stringList = Arrays.asList(strArr); Collections.sort(stringList, (left, right) -> intIndex[stringList.indexOf(left)] - intIndex[stringList.indexOf(right)]); return stringList.toArray(new String[stringList.size()]); } else return strArr; } public static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i + 1] < arr[i]) { return false; }; } return true; } // Driver program to test function. public static void main(String args[]) { int[] indexes = new int[]{0,2,5,8}; String[] sources = new String[]{"how", "are", "today", "you"}; String[] targets = new String[]{"I", "am", "thanks", "fine"}; String[] sortedSources = sortWithIndex(sources,indexes); String[] sortedTargets = sortWithIndex(targets,indexes); Arrays.sort(indexes); System.out.println("Sorted Sources " + Arrays.toString(sortedSources) + " Sorted Targets " + Arrays.toString(sortedTargets) + " Sorted Indexes " + Arrays.toString(indexes)); }
3、使用List和map实现
import java.util.*; import java.lang.*; import java.io.*; public >Test{ public static <T extends Comparable<T>> void sortWithIndex( final List<T> key, List<?>... lists){ if(key == null || lists == null) throw new NullPointerException("Key cannot be null."); for(List<?> list : lists) if(list.size() != key.size()) throw new IllegalArgumentException("All lists should be of the same size"); // 列表的大小为0或1,不需要排序 if(key.size() < 2) return; // 创建索引列表 List<Integer> indices = new ArrayList<Integer>(); for(int i = 0; i < key.size(); i++) indices.add(i); // 根据键对索引列表进行排序 Collections.sort(indices, new Comparator<Integer>(){ @Override public int compare(Integer i, Integer j) { return key.get(i).compareTo(key.get(j)); } }); Map<Integer, Integer> swapMap = new HashMap<Integer, Integer>(indices.size()); List<Integer> swapFrom = new ArrayList<Integer>(indices.size()), swapTo = new ArrayList<Integer>(indices.size()); // 创建允许按N个交换对列表排序的映射。 for(int i = 0; i < key.size(); i++){ int k = indices.get(i); while(i != k && swapMap.containsKey(k)) k = swapMap.get(k); swapFrom.add(i); swapTo.add(k); swapMap.put(i, k); } // 使用交换顺序通过交换元素对每个列表进行排序 for(List<?> list : lists) for(int i = 0; i < list.size(); i++) Collections.swap(list, swapFrom.get(i), swapTo.get(i)); } public static void main (String[] args) throws java.lang.Exception{ List<Integer> index = Arrays.asList(0,2,8,5); List<String> sources = Arrays.asList("how", "are", "today", "you"); // 列表类型不需要相同 List<String> targets = Arrays.asList("I", "am", "thanks", "fine"); sortWithIndex(index, index, sources, targets); System.out.println("Sorted Sources " + sources + " Sorted Targets " + targets + " Sorted Indexes " + index); } }
输出:
Sorted Sources [how, are, you, today] Sorted Targets [I, am, fine, thanks] Sorted Indexes [0, 2, 5, 8]
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。