用1.8 jdk按多个字段排序对象列表

时间:2018-04-18 09:58:16

标签: java sorting comparator

我收集了一些人:

add_action( 'ccs_fetch_list', array( __CLASS__, 'fetch_list' ) ); 

我想通过以下方式对人员进行排序:

  1. 首先显示带有favorite = true的项目
  2. 带计数的第二个显示项> 0
  3. 最后显示项目,包含count == 0
  4. 如何使用1.8 JDK中的Comparator编写此条件并检索预期结果?

2 个答案:

答案 0 :(得分:2)

您可以使用Comparator.comparing(...).thenComparing(...)按多个属性进行排序。要首先对收藏夹进行排序,您可以按该属性排序,但必须反转结果,因为falsetrue之前排序。假设您只想根据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