我收集了一些人:
add_action( 'ccs_fetch_list', array( __CLASS__, 'fetch_list' ) );
我想通过以下方式对人员进行排序:
如何使用1.8 JDK中的Comparator编写此条件并检索预期结果?
答案 0 :(得分:2)
您可以使用Comparator.comparing(...).thenComparing(...)
按多个属性进行排序。要首先对收藏夹进行排序,您可以按该属性排序,但必须反转结果,因为false
在true
之前排序。假设您只想根据count
是> 0
进行排序,而不是按计数的实际值进行排序:
List<Person> persons = Arrays.asList(
new Person("p1", 2, true),
new Person("p2", 0, false),
new Person("p3", 0, true),
new Person("p4", 1, false),
new Person("p5", 3, true));
persons.stream().sorted(Comparator
.comparing(Person::isFavorite).reversed()
.thenComparing(p -> p.getCount() == 0))
.forEach(System.out::println);
结果:
Person(name=p1, count=2, favorite=true)
Person(name=p5, count=3, favorite=true)
Person(name=p3, count=0, favorite=true)
Person(name=p4, count=1, favorite=false)
Person(name=p2, count=0, favorite=false)
请注意,最终的count == 0
条件是多余的(假设count
不能是< 0
)
或者直接按count
排序并在结束时反转一次;这将在示例中的p5
之前对p1
进行排序:
persons.stream().sorted(Comparator
.comparing(Person::isFavorite)
.thenComparing(Person::getCount).reversed())
.forEach(System.out::println);
答案 1 :(得分:1)
假设您想要对人员进行排序而不是过滤他们,您可以执行以下操作:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionCS"].ConnectionString))
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO Visits(Name,Surname,DocType,DocNumber,Gender,Company,Delivery,Entrance,Visiting) VALUES(@Name,@Surname,@DocType,@DocNumber,@Gender,@Company,@Delivery,@Entrance,@Visiting)");
using (SqlCommand com = new SqlCommand(sb.ToString(), con))
{
con.Open();
com.Parameters.Add("@Name", SqlDbType.NVarChar).Value = NameBox.Text.Split(null)[0];
//Add Other Parameter
//...
com.ExecuteScalar();
}
}
之后,对你的人进行分类:
class Person implements Comparable<Person>
{
...
@Override
int compareTo(Person person)
{
// Maye not specified
if (person == null)
return (1);
// Compare 'favorite'
// Favorites first, so flag=true means smaller
if (this.favorite && (!person.favorite))
return (-1);
else if ((!this.favorite) && person.favorite)
return (1);
// Compare 'count'
// Having count > 0 first, so count=0 means greater
// Assuming count is never negative
if ((this.count == 0) && (person.count > 0))
return (1);
if ((this.count > 0) && (person.count == 0))
return (-1);
// At this point, both persons have the same 'favorite' flag
// And both have either count=0 or count>0
// Order by name in that case
return (this.name.compareTo(person.name));
} // compareTo
} // class Person