有一个字符串列表,每个字符串有4个值集,将根据它们对它们进行排序。
1.序列号
2.状态示例:应按顺序(“煮熟”,“煮熟”,“新鲜”,“生”)
3.日期(完整的ISO-8601日期)
4.按字母顺序
我首先要按列表的序列号对它进行排序。然后,如果两个或多个值具有相同的序列号,我想按已定义的字符串顺序对它们进行排序(STATE HERE)。现在,按日期和时间对状态的每个重复类别进行排序。现在,按字母顺序对每个重复的日期/时间进行排序。
因此,基本上需要进行4级排序。
我首先这样排序。我不确定如何前进。
CREATE OR REPLACE FUNCTION createString(name text, phrase text) RETURNS table (descp text) AS
$$
BEGIN
select b.include, b.exclude, c.include, c.exclude
from (TableB b join TableC c on b.name = c.name and b.phrase = c.phrase)
where b.name = name and b.phrase = phrase;
IF b.include = true THEN
NEW := 'b included';
ELSEIF b.exclude = true THEN
NEW := 'b excluded';
ELSEIF c.exclude = true THEN
NEW := 'c excluded';
ELSEIF c.exclude = true THEN
NEW := 'c excluded';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
示例:
List<String> allStrings= new ArrayList<String>();
List<String> rankList= new ArrayList<String>();
List<String> stateList= new ArrayList<String>();
List<String> dateList= new ArrayList<String>();
List<String> rankListCopy = new ArrayList<String>(rankList.size());
rankListCopy.addAll(rankList);
Collections.sort(rankListCopy,Collections.reverseOrder());
for(String el: rankListCopy) {
System.out.println(el);
}
if(rankList.equals(rankListCopy)) {
System.out.println("CARDS ARE ARRANGED IN serial ORDER");
}
else{
System.out.println("CARDS ARE NOT ARRANGED IN serial ORDER"); }
输出:
String rank state date alphabetically
Europe 2 raw 2019-06-12T09:00:00Z
India 5 raw 2018-06-12T09:00:00Z
new york 5 boiled 2020-07-12T09:00:00Z
US 0 cooked 2020-06-12T09:00:00Z
china 0 cooked 2020-06-12T09:00:00Z
答案 0 :(得分:-1)
我不确定这是否是您所需要的。但是,如果您可以将该事物表示为试图将其排序为具有属性的对象的集合,则可以执行类似的操作。您可以在Comparator中编写所需的比较逻辑,也可以使您的类Comparable并重写compareTo()方法。无论如何,这是Comparator的命题。使用一个小技巧,枚举自然顺序是定义枚举元素的顺序。在您的情况下,枚举字段应按以下顺序排列:(“煮熟”,“煮沸”,“新鲜”,“原始”)
public class Main {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
String dateAsString = df.format(new Date());
List<String> allStrings = Arrays.asList("Europe", "India", "New York", "US", "China");
List<String> rankList = Arrays.asList("2", "5", "5", "0", "0");
List<String> stateList = Arrays.asList("RAW", "RAW", "BOILED", "COOKED", "COOKED");
List<String> dateList = Arrays.asList(dateAsString, dateAsString, dateAsString, dateAsString, dateAsString);
List<YourSortableEntity> inputData = new ArrayList<>();
for(int i =0; i < allStrings.size(); i++) {
try {
Date date = df.parse(dateList.get(i));
inputData.add(new YourSortableEntity(
allStrings.get(i),
Integer.valueOf(rankList.get(i)),
State.valueOf(stateList.get(i)),
date
));
} catch (ParseException e) {
throw new RuntimeException("Could not parse string into date: " + dateList.get(i), e);
}
}
Comparator<YourSortableEntity> comparator = Comparator.comparing(it -> it.getString());
comparator = comparator.thenComparing(it -> it.getRank()).thenComparing(it -> it.getState()).thenComparing(it -> it.getDate());
TreeSet<YourSortableEntity> treeSet = new TreeSet<>(comparator);
treeSet.addAll(inputData);
System.out.println(treeSet);
}
public static class YourSortableEntity {
private final String string;
private final int rank;
private final State state;
private final Date date;
public YourSortableEntity(String string, int rank, State state, Date date) {
this.string = string;
this.rank = rank;
this.state = state;
this.date = date;
}
public String getString() {
return string;
}
public int getRank() {
return rank;
}
public State getState() {
return state;
}
public Date getDate() {
return date;
}
@Override
public String toString() {
return string;
}
}
public enum State {
COOKED,
BOILED,
FRESH,
RAW
}
}