Java排序基于两列

编程入门 行业动态 更新时间:2024-10-28 08:21:39
本文介绍了Java排序基于两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有这样的表格:

String | Int1 | Int2 foo5 0 faa4 1 zaa0 1 zoo4 2 laa4 3 loo1 4

我想得到的是这样的表格:

字符串| Int1 | Int2 foo5 0 laa4 3 zoo4 2 faa4 1 loo1 4 zaa0 1

首先发生的是基于列 Int1的排序。

发生的第二件事情是基于列 Int2 ,但仅限于列中具有相同数字的行 Int1

如何在不使用任何数据库引擎的情况下解决此问题?

解决方案

您通常使用列表< Item> 其中 Item 是一个包含所有三个值的类型(例如,foo,第一行为5,0)。

<然后你会写一个比较器< Item> ,它比较了两个 Item 对象的Int1值。它在比较,如果这给出了明确答案,则返回该答案......并以其他方式比较Int2值es。

Lets say I have table like this:

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

What I would like to get is table like this:

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

First thing that happens is sort based on column Int1.

Second thing that happens is sort of based on column Int2 but only on rows that have same numbers in column Int1

How should I approach this problem without using any database engine?

解决方案

You'd normally do this with a List<Item> where Item is a type containing all three values ("foo", 5, 0 for the first row, for example).

You'd then write a Comparator<Item> which compared the Int1 values of the two Item objects presented to it in compare, and if that gave a definite answer, returned that answer... and otherwise compared the Int2 values.

更多推荐

Java排序基于两列

本文发布于:2023-10-28 02:34:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!