简体   繁体   English

为我的工作面试代码示例编写比较器所需的帮助

[英]Help needed to write a comparator for my job interview code sample

I need help to write a comparator :- 我需要帮助来编写比较器:-

I want this output :- 我想要这个输出:-

Martin Joseph Male 4/2/1979 Green 
Ramya  Patil  Female 5/4/2009 Red
Don    kelly  Male   5/6/1986 Yellow
Van    Shinde female 3/4/1984 Green

But i am getting the following output :- 但是我得到以下输出:-

Output 1:
 Van    Shinde female 3/4/1984 Green
 Don    kelly  Male   5/6/1986 Yellow
 Ramya  Patil  Female 5/4/2009 Red
 Martin Joseph Male 4/2/1979 Green 

how do i sort on last name keeping the order of the list intact .. i mean i want to output female first sorting on last name and then male sorted on last name ... please help me guys .. 我该如何按姓氏排序,以保持列表顺序不变..我的意思是我要输出按姓氏排序的女性优先,然后按姓氏排序的男性...请帮助我。

this is the comparator i am using after i use the gender comparator :- 这是我使用性别比较器后使用的比较器:-

public class LastNameComparator implements Comparator<Person> {


public int compare(Person name_one, Person name_two) {
        // TODO Auto-generated method stub
        if(name_one.getGender().equals(name_two.getGender())){
            return name_one.getLast_name().compareTo(name_two.getLast_name());

        }else{
        return name_one.getLast_name().compareTo(name_two.getLast_name());
        }
    }

}
public int compare(Person a, Person b) {
  int ret = a.getGender().compareTo(b.getGender());
  if (ret == 0) {
    ret = a.getLastName().compareTo(b.getLastName());
  }
  return ret;
}

When I read the code in your OP I see that you're always returning the result of comparing by the last name: 当我在您的OP中读取代码时,我看到您总是返回以姓氏进行比较的结果:

If genders are equal
  then compare last names
  else compare last names

However that doesn't much the experimental results which you say you're getting; 但这并不能说明您说得到的实验结果。 so as well as modifying your code to match Cletus' suggestion, maybe something else is wrong with your code as well. 因此,除了修改您的代码以符合Cletus的建议外,您的代码也可能有其他问题。

Cletus' solution is probably the most appropriate here, but if you want to take it a little further... Cletus的解决方案在这里可能是最合适的,但是如果您想进一步了解它的话...

LastNameComparator is rather misleadingly named, since it actually compares on gender as well as last name. LastNameComparator名称颇具误导性,因为它实际上比较性别和姓氏。 It might be less ambiguous to split this functionality into two separate Comparators : 将此功能拆分为两个单独的Comparators可能不太明确:

class GenderComparator implements Comparator<Person> {
    public int compare(Person a, Person b) {
        return a.getGender().compareTo(b.getGender());
    }
}

class LastNameComparator implements Comparator<Person> {
    public int compare(Person a, Person b) {
        return a.getLastName().compareTo(b.getLastName());
    }
}

Then, combine the functionality in a 'custom' Comparator : 然后,将功能合并到“自定义” Comparator

Comparator<Person> genderThenLastNameComparator = new Comparator<Person>() {
    public int compare(Person a, Person b) {
        int result = new GenderComparator().compare(a, b);
        return result == 0 ? new LastNameComparator().compare(a, b) : result;
    }
}

It's more complicated, but more flexible and reusable. 它更复杂,但更灵活且可重用。 (Which may or may not be justified for such a simple case.) (对于这种简单情况,可能会或可能不会。)

As a complement to cletus's answer, here's a version using a CompareToBuilder (from Apache Commons Lang): 作为对cletus答案的补充,这是一个使用CompareToBuilder的版本(来自Apache Commons Lang):

public int compare(Person lhs, Person rhs) {
    return new CompareToBuilder()
            .append(lhs.getGender(), rhs.getGender())
            .append(lhs.getLastName(), rhs.getLastName())
            .toComparison();
}

This should be relatively simple: 这应该相对简单:

here are the rules you specified: 这是您指定的规则:

  • females before males 女性先于男性
  • names sorted in last name ascending 名字按姓氏升序排列

you do something like this: 你做这样的事情:

// if the genders are not equal, return <0 if this object's gender is female
// else 
//    return lastName.compareTo( obj.lastName );

that's all you need to implement this comparator... 这就是实现此比较器所需的全部...

then when you want to sort it, use the collections framework sort! 然后,当您想对其进行排序时,请使用collections框架进行排序!

public static <T> void sort(List<T> list,
                            Comparator<? super T> c)

This is a stable sort too...which is awesome. 这也是一个稳定的排序。。。 If you answer it this way I'm sure you'll get the job. 如果您以这种方式回答,我相信您会得到这份工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM