Java 排序数组并保留索引
使用Java 排序数组并保留索引,java,arrays,sorting,indexof,Java,Arrays,Sorting,Indexof,使用Arrays.sort(数组)是对数组进行排序的一种非常简单的方法。但有一个问题。对于每个未排序的数组,都有一个索引,该索引引用其他数据结构中的对象。对数组进行排序时(使用上述代码或任何其他类似方式),每个值的索引都将更改。是否有一种解决方案可以为每个值指定索引 编辑 由于数组中有许多相同的值,因此使用HashMap将不会有用。对两个数据结构并行排序 或者将引用索引(用于引用其他数据结构)设置为每个元素的内部字段,以便每个元素都保持相同的“引用索引”,即使通过排序更改了元素的顺序 或者更好的
Arrays.sort(数组)
是对数组进行排序的一种非常简单的方法。但有一个问题。对于每个未排序的数组,都有一个索引,该索引引用其他数据结构中的对象。对数组进行排序时(使用上述代码或任何其他类似方式),每个值的索引都将更改。是否有一种解决方案可以为每个值指定索引
编辑
由于数组中有许多相同的值,因此使用HashMap将不会有用。对两个数据结构并行排序 或者将引用索引(用于引用其他数据结构)设置为每个元素的内部字段,以便每个元素都保持相同的“引用索引”,即使通过排序更改了元素的顺序 或者更好的是,只需给每个元素一个内部字段,该字段只是另一个数据结构中元素的直接引用变量。(除非出于某种原因需要原始索引本身,而不仅仅是对关联对象/值的引用)
例如:首先,如果有两个平行数组,其中的元素位于相应的索引中,则表示您可能希望将数据结构更改为一个数组,其中容器对象包含两个字段。然后,该容器对象将实现可比较的接口 但是,坚持你所说的,一种方法可能是:
/**
* Sorts parallel arrays in-place. Sorted by the first array and updating
* all other arrays to match.
* Uses the natural sorting of the objects.
* All arrays must be the same length.
*
* @param keys the values used to sort, may be duplicate
*
* @param otherArrays the arrays to have reordered to match the sorting of
* the keys array.
*
* @exception IllegalArgumentException if any of otherArrays have a length
* different that the keys array.
*/
public static <E extends Comparable<? super E>> void sortParallelArrays(
E[] keys,
Object[] ... otherArrays
) {
int numKeys = keys.length;
int numOtherArrays = otherArrays.length;
for(Object[] otherArray : otherArrays) {
if(otherArray.length != numKeys) {
throw new IllegalArgumentException("Mismatched array lengths");
}
}
// A list of all indexes per key
// This also does the sorting within the TreeMap using natural ordering
SortedMap<E, List<Integer>> originalIndexesByKey = new TreeMap<E, List<Integer>>();
// Populate the map
for(int i = 0; i < numKeys; i++) {
E key = keys[i];
List<Integer> originalIndexes = originalIndexesByKey.get(key);
if(originalIndexes == null) {
// Optimization for the non-duplicate keys
originalIndexesByKey.put(key, Collections.singletonList(i));
} else {
if(originalIndexes.size() == 1) {
// Upgrade to ArrayList now that know have duplicate keys
originalIndexes = new ArrayList<Integer>(originalIndexes);
originalIndexesByKey.put(key, originalIndexes);
}
originalIndexes.add(i);
}
}
// Store back to keys and sort other arrays in a single traversal
Object[][] sortedOtherArrays = new Object[numOtherArrays][numKeys];
int pos = 0;
for(Map.Entry<E, List<Integer>> entry : originalIndexesByKey.entrySet()) {
E key = entry.getKey();
for(int index : entry.getValue()) {
keys[pos] = key;
for(int ooIndex = 0; ooIndex < numOtherArrays; ooIndex++) {
sortedOtherArrays[ooIndex][pos] = otherArrays[ooIndex][index];
}
pos++;
}
}
assert pos == numKeys : "Arrays should be full";
// Copy back to original arrays for in-place sort
for(int ooIndex = 0; ooIndex < numOtherArrays; ooIndex++) {
System.arraycopy(
sortedOtherArrays[ooIndex], 0,
otherArrays[ooIndex], 0,
numKeys);
}
}
/**
*将并行数组排序到位。按第一个数组排序并更新
*要匹配的所有其他数组。
*使用对象的自然排序。
*所有数组的长度必须相同。
*
*@param key用于排序的值可能重复
*
*@param otherarray要重新排序以匹配的排序的数组
*密钥数组。
*
*@exception IllegalArgumentException如果任何其他数组具有长度
*与密钥数组不同。
*/
public static可能您需要一个
数据结构?@strasher有许多相同的值,因此密钥的值将被覆盖。我的意思是如果你指的是HashMap。