[英]Sort a list with element still in first position
I have a String list:我有一个字符串列表:
List<String> listString = new ArrayList<String>();
listString.add("faq");
listString.add("general");
listString.add("contact");
I do some processing on the list and I want to sort this list but I want "general" to always end up in first position.我对列表做了一些处理,我想对这个列表进行排序,但我希望“一般”总是排在第一位。 Thx ;)
谢谢 ;)
I like @Petar's approach, but another approach would be to sort it using a custom Comparator that always said that "general" was before whatever it was being compared to.我喜欢@Petar 的方法,但另一种方法是使用自定义比较器对其进行排序,该比较器总是说“一般”在被比较之前。
Collections.sort(list, new Comparator<String>()
{
int compare(String o1, String o2)
{
if (o1.equals(o2)) // update to make it stable
return 0;
if (o1.equals("general"))
return -1;
if (o2.equals("general"))
return 1;
return o1.compareTo(o2);
}
});
Do Collections.sort
on its subList
instead.改为在其
subList
上执行Collections.sort
。
List<String> list = new ArrayList<String>(
Arrays.asList("Zzz...", "Two", "One", "Three")
);
Collections.sort(list.subList(1, list.size()));
System.out.println(list);
// "[Zzz..., One, Three, Two]"
subList(int fromIndex, int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive.fromIndex
和toIndex
之间的部分的视图。 The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. If the special element is not at index 0, then simply put it there before you sort it as follows:如果特殊元素不在索引 0 处,则只需将其放在那里,然后按如下方式对其进行排序:
List<String> list = new ArrayList<String>(
Arrays.asList("Four", "Five", "Zzz...", "Two", "One", "Three")
);
Collections.swap(list, list.indexOf("Zzz..."), 0);
Collections.sort(list.subList(1, list.size()));
System.out.println(list);
// "[Zzz..., Five, Four, One, Three, Two]"
Collections.swap(List<?> list, int i, int j)
对列表进行排序而不在其中包含“常规”,然后将其添加到开头。
You can use the following code snippet but it might have perfomance/memory problems for the very big lists.您可以使用以下代码片段,但对于非常大的列表,它可能存在性能/内存问题。
public static List<String> sortSpecial(List<String> list, final String alwaysOnTopItem) {
list.remove(alwaysOnTopItem);
Collections.sort(list);
List<String> result = new ArrayList<String>(list.size() + 1);
result.add(alwaysOnTopItem);
result.addAll(list);
return result;
}
public static void main(String[] args) {
List<String> listString = new ArrayList<String>();
listString.add("faq");
listString.add("general");
listString.add("contact");
String alwaysOnTopItem = "general";
List<String> sortedList = sortSpecial(listString, alwaysOnTopItem);
System.out.println(sortedList);
}
Nice!好的! Question.
题。 If we want to fix the positions for the first and last elements then we can enhance the
@Paul Tomblin's
approach as like below one.如果我们想固定第一个和最后一个元素的位置,那么我们可以增强
@Paul Tomblin's
方法,如下所示。 Please copy the code and try it.请复制代码并尝试。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomComparatorToFixedPosition {
public static void main(String[] args) {
List<Emp> empNames = new ArrayList<>();
empNames.add(new Emp("name1"));
empNames.add(new Emp("LastName"));
empNames.add(new Emp("name2"));
empNames.add(new Emp("FirstName"));
empNames.add(new Emp("name3"));
getSortedName(empNames).stream().forEach(emp -> System.out.println(emp.getName()));
}
public static List<Emp> getSortedName(List<Emp> empNames) {
String first = "FirstName";
String last = "LastName";
Collections.sort(empNames, new Comparator<Emp>() {
public int compare(Emp o1, Emp o2) {
if (o1.getName().equalsIgnoreCase(first) || o2.getName().equalsIgnoreCase(last))
return -1;
if (o2.getName().equalsIgnoreCase(first) || o1.getName().equalsIgnoreCase(last))
return 1;
return o1.getName().compareTo(o2.getName());
}
});
return empNames;
}
}
class Emp {
private String name;
Emp(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.