多次排序 Arraylist
Sort an Arraylist multiple times
我有一个 class "SendBundleQuery",其中包含一些对象,例如类别、类型、名称。类别可以是两种类型 base 和 Addons 。类型可以是数据、语音、短信三种类型。我有一个这个 class 对象的数组列表,现在我需要这样排序,就像所有基本优惠都排在第一位,然后是所有插件。对于所有基本报价,订单将是数据、语音、短信。
目前我已经按类别排序了
public int compareTo(SendBundleQuery other) {
int result= category.compareTo(other.category);
if(result==0){
result =other.bundleType.compareTo(bundleType);
}
return result;
}
但是现在我需要达到上面的条件。有什么好的方法呢。
下面是我想要实现的例子。
我需要为客户编写短信文本如下:
尊敬的客户,
You have <0 MB> left within your <Eenmalig 100 MB Maandbundel>. ---Base offer
In addition, you have <22 Minuten> left within your <100 Minuten Bundel>. --- Base offer
In addition, you have <0 MB> left within your <Web 200 MB Maandbundel>. --Addon
In addition, you have <35 MB> left within your <Alles-in-1 op Reis Data Dagbundel>. --Addon
In addition, you have <374 MB> left within your <Blox 400 MB Maandbundel>. --Addon
In addition, you have <20 Minuten> left within your <Alles-in-1 Op Reis 20 Minuten gesprekken ontvangen>. --Addon
In addition, you have <20 Minuten> left within your <Alles-in-1 Op Reis 20 Minuten Bellen>. --Addon
In addition, you have <20 SMS> left within your <Alles-in-1 Op Reis 20 SMS Dagbundel>. --Addon
这些学分更新至 <12-12-2014> <14.53>。
在我的沃达丰中维护您的 BloX 和其他设备。
以下是当前结果,但顺序是数据、短信然后是语音,但我需要数据、语音然后是短信:
Dear Customer,
You have 0.0 MB Data left within your Web 500 MB Maandbundel.
In addition, you have 106 minutes left within your 150 Minuten Bundel.
In addition, you have 35.0 MB Data left within your Alles-in-1 op Reis Data Dagbundel.
In addition, you have 20 messages left within your Alles-in-1 Op Reis 20 SMS Dagbundel.
In addition, you have 20 minutes left within your Alles-in-1 Op Reis 20 Minuten gesprekken ontvangen.
In addition, you have 20 minutes left within your Alles-in-1 Op Reis 20 Minuten Bellen.
These credits are updated until 15-12-2014 at 03:57.
Maintain your BloX and extras in My Vodafone.
为了按多个条件排序,写compareTo
如下:
public int compareTo(SendBundleQuery other) {
int result = category1.compareTo(other.category1);
if( result == 0 ) {
result = category2.compareTo(other.category2);
}
return result;
}
如果您有多个条件要检查,请按重要性顺序检查它们。每当比较结果为 0(即;在那种情况下它们相等)时,继续检查下一个。
一个空的例子,因为我没有足够的信息根据你的 post:
给你准确的代码
public int compareTo(SomeObject other) {
int comparison1 = someProperty.compareTo(other.someProperty);
if( comparison1 != 0 ) {
return comparison1; // the highest priority ordening is leading
}
int comparison2 = someLessImportantProperty.compareTo(other.someLessImportantProperty);
if( comparison2 != 0 ) {
return comparison2; // the second highest priority ordening is leading
}
// if neither of the more important ones matter; sort by the least important one
return someUnImportantProperty.compareTo(other.someUnImportantProperty);
}
Since Java 8 方法引用和 lambda 为常见的排序要求添加了一组额外的优雅方法。其中之一就是您所需要的:首先根据 属性 x 进行排序,如果不可用,则根据 属性 y.
进行排序
humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
(提供完整示例 here)。
这将根据两个 getter 方法对 Human
对象列表进行排序。
对您来说,Human
是 SendBundleQuery
,而 getter 是您要求的排序。
我有一个 class "SendBundleQuery",其中包含一些对象,例如类别、类型、名称。类别可以是两种类型 base 和 Addons 。类型可以是数据、语音、短信三种类型。我有一个这个 class 对象的数组列表,现在我需要这样排序,就像所有基本优惠都排在第一位,然后是所有插件。对于所有基本报价,订单将是数据、语音、短信。
目前我已经按类别排序了
public int compareTo(SendBundleQuery other) {
int result= category.compareTo(other.category);
if(result==0){
result =other.bundleType.compareTo(bundleType);
}
return result;
}
但是现在我需要达到上面的条件。有什么好的方法呢。
下面是我想要实现的例子。
我需要为客户编写短信文本如下:
尊敬的客户,
You have <0 MB> left within your <Eenmalig 100 MB Maandbundel>. ---Base offer
In addition, you have <22 Minuten> left within your <100 Minuten Bundel>. --- Base offer
In addition, you have <0 MB> left within your <Web 200 MB Maandbundel>. --Addon
In addition, you have <35 MB> left within your <Alles-in-1 op Reis Data Dagbundel>. --Addon
In addition, you have <374 MB> left within your <Blox 400 MB Maandbundel>. --Addon
In addition, you have <20 Minuten> left within your <Alles-in-1 Op Reis 20 Minuten gesprekken ontvangen>. --Addon
In addition, you have <20 Minuten> left within your <Alles-in-1 Op Reis 20 Minuten Bellen>. --Addon
In addition, you have <20 SMS> left within your <Alles-in-1 Op Reis 20 SMS Dagbundel>. --Addon
这些学分更新至 <12-12-2014> <14.53>。
在我的沃达丰中维护您的 BloX 和其他设备。
以下是当前结果,但顺序是数据、短信然后是语音,但我需要数据、语音然后是短信:
Dear Customer,
You have 0.0 MB Data left within your Web 500 MB Maandbundel.
In addition, you have 106 minutes left within your 150 Minuten Bundel.
In addition, you have 35.0 MB Data left within your Alles-in-1 op Reis Data Dagbundel.
In addition, you have 20 messages left within your Alles-in-1 Op Reis 20 SMS Dagbundel.
In addition, you have 20 minutes left within your Alles-in-1 Op Reis 20 Minuten gesprekken ontvangen.
In addition, you have 20 minutes left within your Alles-in-1 Op Reis 20 Minuten Bellen.
These credits are updated until 15-12-2014 at 03:57.
Maintain your BloX and extras in My Vodafone.
为了按多个条件排序,写compareTo
如下:
public int compareTo(SendBundleQuery other) {
int result = category1.compareTo(other.category1);
if( result == 0 ) {
result = category2.compareTo(other.category2);
}
return result;
}
如果您有多个条件要检查,请按重要性顺序检查它们。每当比较结果为 0(即;在那种情况下它们相等)时,继续检查下一个。
一个空的例子,因为我没有足够的信息根据你的 post:
给你准确的代码public int compareTo(SomeObject other) {
int comparison1 = someProperty.compareTo(other.someProperty);
if( comparison1 != 0 ) {
return comparison1; // the highest priority ordening is leading
}
int comparison2 = someLessImportantProperty.compareTo(other.someLessImportantProperty);
if( comparison2 != 0 ) {
return comparison2; // the second highest priority ordening is leading
}
// if neither of the more important ones matter; sort by the least important one
return someUnImportantProperty.compareTo(other.someUnImportantProperty);
}
Since Java 8 方法引用和 lambda 为常见的排序要求添加了一组额外的优雅方法。其中之一就是您所需要的:首先根据 属性 x 进行排序,如果不可用,则根据 属性 y.
进行排序humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
(提供完整示例 here)。
这将根据两个 getter 方法对 Human
对象列表进行排序。
对您来说,Human
是 SendBundleQuery
,而 getter 是您要求的排序。