按相同标准对两个int []进行排序

时间:2021-08-21 14:05:50

I have two int[], Start and End. They refer to a series of lines: Start[0] is the start position of line 0 and End[0] is to the end position of line 0. I want to sort Start in ascending order and then sort End in the same way, so the arrays stay aligned.

我有两个int [],Start和End。它们引用一系列行:Start [0]是第0行的起始位置,End [0]是第0行的结束位置。我想按升序对Start进行排序,然后以相同的方式对End进行排序,所以阵列保持对齐。

In MatLab, you can do this:

在MatLab中,您可以这样做:

[idx, StartSorted] = sort(Start); EndSorted = End(idx);

[idx,StartSorted] = sort(开始); EndSorted =结束(idx);

What is the best way to do the same operation in Java? Write a tuple class, or is there a better way?

在Java中执行相同操作的最佳方法是什么?写一个元组课,还是有更好的方法?

In general, what is the best way to manipulate paired data like this?

一般来说,操作这样的配对数据的最佳方法是什么?

Thak you.

亲爱的


Thanks guys, that's great.

谢谢你们,这太好了。

2 个解决方案

#1


3  

The best way would be, as you stated, to create a Tuple class of some sort that maintains both values. You should also implement equals and make your class implement Comparable<T> and implement compareTo. Once you have done that, you can sort a collection of Tuple instances (using Collections.sort) and they will be sorted according to the ordering specified by your compareTo method. In this particular instance, your comparison will simply be based on the value of start in Tuple.

正如你所说,最好的方法是创建一个维持这两个值的元组类。您还应该实现equals并使您的类实现Comparable 并实现compareTo。完成后,您可以对一组Tuple实例进行排序(使用Collections.sort),它们将根据compareTo方法指定的顺序进行排序。在这个特定的实例中,您的比较将仅基于元组中start的值。

If you don't care so much about defining implicit ordering for your class by implementing Comparable<T>, you can specify an ad-hoc comparator using Comparator#comparingInt and use it with an array of your objects:

如果您不太关心通过实现Comparable 为类定义隐式排序,则可以使用Comparator#comparisoningnt指定ad-hoc比较器,并将其与对象数组一起使用:

//tuples is an array
//Tuple::getStart is a method reference to the getStart method
//in tuple. So you get an ad-hoc comparator that uses this
//function as the basis for ordering
Arrays.sort(tuples, Comparator.comparingInt(Tuple::getStart)

#2


5  

Don't use two arrays of int. Use a single array of Line, where Line would be a class having a start property and an end property.

不要使用两个int数组。使用单个Line数组,其中Line将是具有start属性和end属性的类。

Then, to sort lines by their start (in Java 8):

然后,按照它们的开始对行进行排序(在Java 8中):

Arrays.sort(lines, Comparator.comparingInt(Line::getStart));

#1


3  

The best way would be, as you stated, to create a Tuple class of some sort that maintains both values. You should also implement equals and make your class implement Comparable<T> and implement compareTo. Once you have done that, you can sort a collection of Tuple instances (using Collections.sort) and they will be sorted according to the ordering specified by your compareTo method. In this particular instance, your comparison will simply be based on the value of start in Tuple.

正如你所说,最好的方法是创建一个维持这两个值的元组类。您还应该实现equals并使您的类实现Comparable 并实现compareTo。完成后,您可以对一组Tuple实例进行排序(使用Collections.sort),它们将根据compareTo方法指定的顺序进行排序。在这个特定的实例中,您的比较将仅基于元组中start的值。

If you don't care so much about defining implicit ordering for your class by implementing Comparable<T>, you can specify an ad-hoc comparator using Comparator#comparingInt and use it with an array of your objects:

如果您不太关心通过实现Comparable 为类定义隐式排序,则可以使用Comparator#comparisoningnt指定ad-hoc比较器,并将其与对象数组一起使用:

//tuples is an array
//Tuple::getStart is a method reference to the getStart method
//in tuple. So you get an ad-hoc comparator that uses this
//function as the basis for ordering
Arrays.sort(tuples, Comparator.comparingInt(Tuple::getStart)

#2


5  

Don't use two arrays of int. Use a single array of Line, where Line would be a class having a start property and an end property.

不要使用两个int数组。使用单个Line数组,其中Line将是具有start属性和end属性的类。

Then, to sort lines by their start (in Java 8):

然后,按照它们的开始对行进行排序(在Java 8中):

Arrays.sort(lines, Comparator.comparingInt(Line::getStart));