Java sorting based on two columns

Say I have such a watch:

String | Int1 | Int2
 "foo"    5      0
 "faa"    4      1
 "zaa"    0      1
 "zoo"    4      2
 "laa"    4      3
 "loo"    1      4

What I want is this table:

String | Int1 | Int2
 "foo"    5      0
 "laa"    4      3
 "zoo"    4      2
 "faa"    4      1
 "loo"    1      4
 "zaa"    0      1

The first thing that happens is to sort based on column INT1

The second thing that happens is based on column INT2, but only rows with the same number in column INT1

How can I fix this problem without using any database engine?

Solution

You usually use the list < item > where item is a type that contains all three values (for example, "foo", 5,0 in the first row)

Then, you will write a comparator < item > to compare the INT1 values of the two item objects presented to it. If an explicit answer is given, the answer will be returned. Otherwise, the INT2 value will be compared

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>