使用Comparator排序列表时出错

时间:2015-06-10 10:00:43

标签: java comparator

我正在尝试根据我的name属性对对象列表进行排序。所以我创建了一个比较器,我注意到它没有被排序。如果使用此错误,请告知。

List<Country> countryList = new ArrayList<Country>();
Country country1 = new Country("TEST1","Washington");
Country country2 = new Country ("TEST10", New Delhi");
Country country3= new Country ("TEST9", London");
countryList.add(country1);
countryList.add(country2);

Collections.sort(countryList,new Comparator<Country>() {
            public int compare(Country o1, Country o2) {
                return o1.getName().compareTo(o2.getName());
            }
});

我正在退出,应该是其他方式。

    TEST1 : Washington
    TEST10 : New Delhi
    TEST9 : London

预期

    TEST1 : Washington
    TEST9 : London
    TEST10 : New Delhi

3 个答案:

答案 0 :(得分:2)

按字母顺序排列TEST10在TEST9之前

答案 1 :(得分:2)

您正在尝试按姓名排序。但名字是TEST1,TEST10和TEST9。比较时,您将按字母顺序获得订单:

TEST1
TEST10
TEST9

您可以尝试以下代码:

Collections.sort(countryList,new Comparator<Country>() {
                    public int compare(Country o1, Country o2) {
                        if (o1.getName().length() > o2.getName().length()) return 1;
                        if (o1.getName().length() < o2.getName().length()) return -1;

                        return o1.getName().compareTo(o2.getName());
                    }
        });

答案 2 :(得分:2)

compareTo lexicographically使用Collections.sort(countryList,new Comparator<Country>() { public int compare(Country o1, Country o2) { if (o1.getName().length() > o2.getName().length()) return 1; if (o1.getName().length() < o2.getName().length()) return -1; return o1.getName().compareTo(o2.getName()); } }); 来比较字符串,但您的逻辑需要比较 长度 of string:

#import "IndoorsTestVC.h"
#import "IndoorsTestDelegate.h"
#import <Indoors/Indoors.h>
#import <IndoorsSurface/ISIndoorsSurfaceViewController.h>

@interface IndoorsTestVC ()

@property (strong, nonatomic) IndoorsTestDelegate *del1;
@property (strong, nonatomic) IndoorsTestDelegate *del2;

@end

@implementation IndoorsTestVC

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.del1 = [[IndoorsTestDelegate alloc]
        initWithController:self
                   andName:@"Del1"];
    self.del2 = [[IndoorsTestDelegate alloc]
        initWithController:self
                   andName:@"Del2"];

    __unused Indoors *indoors = [[Indoors alloc]
        initWithLicenseKey:API_KEY
        andServiceDelegate:self.del1];
    [[Indoors instance] enableEvaluationMode:NO];
    [[Indoors instance] setLogLevel:IDSLogLevelWarning];
    [[Indoors instance] registerLocationListener:self.del2];

    ISIndoorsSurfaceViewController *surfaceVC = [[ISIndoorsSurfaceViewController alloc] init];
    surfaceVC.delegate = self.del1;

    // add surfaceVC as a child view controller
    [self addChildViewController:surfaceVC];
    [self addSubview:surfaceVC.view
        withEqualSizeLikeParent:self.view];
    [surfaceVC didMoveToParentViewController:self];

    // load building
    [surfaceVC loadBuildingWithBuildingId:1234];

}

- (void)dealloc {
    [[Indoors instance] removeLocationListener:self.del2];
    NSLog(@"Dealloc test controller %@", _title);
}