排序的拆分,
//order by price or time
//1,高亮状态2,升降顺序
-(void)sortByTime:(id)sender
{
NSArray *arr = [self.fListArray sortedArrayUsingComparator:^NSComparisonResult(id item1, id item2) {
HFFlight *flight1 = item1;
HFFlight *flight2 = item2;
//timebutton 排序
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSDate *date1 = [dateFormatter dateFromString:flight1.orgDateTime1];
NSDate *date2 = [dateFormatter dateFromString:flight2.orgDateTime1];
NSComparisonResult result = [date1 compare:date2];
if (timeButtonIsDescending) {
if (result == NSOrderedAscending) {
result = NSOrderedDescending;
}else if (result == NSOrderedDescending) {
result = NSOrderedAscending;
}else{
result= NSOrderedSame;
}
}
return result;
}];
//设置timebutton的状态,从高亮--》非高亮
if (priceButtonIsDescending) {
priceButtonIsHighlighted = NO;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_down.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}else{
priceButtonIsHighlighted = NO;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_up.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}
if (timeButtonIsDescending && timeButtonIsHighlighted) {
timeButtonIsDescending = NO;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}elseif(timeButtonIsHighlighted && !timeButtonIsDescending) {
timeButtonIsDescending = YES;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}elseif(!timeButtonIsHighlighted && timeButtonIsDescending){
timeButtonIsHighlighted = YES;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}else{
timeButtonIsHighlighted = YES;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:arr];
self.fListArray = mutableArray;
[self.expansionTableViewreloadData];
}
-(void)sortByPrice:(id)sender
{
NSArray *arr = [self.fListArray sortedArrayUsingComparator:^NSComparisonResult(id item1, id item2) {
HFFlight *flight1 = item1;
HFFlight *flight2 = item2;
HFCabin *cabin1 = [flight1.cabins objectAtIndex:0];
HFCabin *cabin2 = [flight2.cabins objectAtIndex:0];
if (priceButtonIsDescending) {//降序
if (cabin1.adultPrice > cabin2.adultPrice) {
return NSOrderedDescending;
} else if (cabin1.adultPrice < cabin2.adultPrice) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}else{
if (cabin1.adultPrice > cabin2.adultPrice) {
return NSOrderedAscending;
} else if (cabin1.adultPrice < cabin2.adultPrice) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
}];
//设置timebutton的状态,从高亮--》非高亮
if (timeButtonIsDescending) {
timeButtonIsHighlighted = NO;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_up.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}else{
timeButtonIsHighlighted = NO;
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_down.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}
if (priceButtonIsHighlighted && priceButtonIsDescending) {
priceButtonIsDescending = NO;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}elseif(priceButtonIsHighlighted && !priceButtonIsDescending) {
priceButtonIsDescending = YES;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}elseif(!priceButtonIsHighlighted && priceButtonIsDescending){
priceButtonIsHighlighted = YES;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}else{
priceButtonIsHighlighted = YES;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:arr];
self.fListArray = mutableArray;
[self.expansionTableViewreloadData];
}
-(void)sortFilght:(id)sender
{
UIButton *btn = (UIButton *)sender;
if (btn.tag == 1) {
if (priceButtonFirstIn) {
if(priceButtonIsDescending){
priceButtonIsDescending = NO;
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_down.png"] forState:UIControlStateNormal];
}else{
priceButtonIsDescending = YES;
[btn setImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
}
priceButtonFirstIn = NO;
timeButtonFirstIn = YES;
}
}else{
if (timeButtonFirstIn) {
if(timeButtonIsDescending){
timeButtonIsDescending = NO;
}else{
timeButtonIsDescending = YES;
}
timeButtonFirstIn = NO;
priceButtonFirstIn = YES;
}
}
NSArray *arr = [self.fListArray sortedArrayUsingComparator:^NSComparisonResult(id item1, id item2) {
HFFlight *flight1 = item1;
HFFlight *flight2 = item2;
HFCabin *cabin1 = [flight1.cabins objectAtIndex:0];
HFCabin *cabin2 = [flight2.cabins objectAtIndex:0];
if (btn.tag == 1) {//pricebutton 排序
if (priceButtonIsDescending) {//升序
if (cabin1.adultPrice > cabin2.adultPrice) {
return NSOrderedDescending;
} else if (cabin1.adultPrice < cabin2.adultPrice) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}else{//降序
if (cabin1.adultPrice > cabin2.adultPrice) {
return NSOrderedAscending;
} else if (cabin1.adultPrice < cabin2.adultPrice) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
}else{//timebutton 排序
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSDate *date1 = [dateFormatter dateFromString:flight1.orgDateTime1];
NSDate *date2 = [dateFormatter dateFromString:flight2.orgDateTime1];
NSComparisonResult result = [date1 compare:date2];
if (timeButtonIsDescending) {
if (result == NSOrderedAscending) {
result = NSOrderedDescending;
}else if (result == NSOrderedDescending) {
result = NSOrderedAscending;
}else{
result= NSOrderedSame;
}
}
return result;
}
}];
if (btn.tag == 1)//pricebutton.tag = 1
{//pricebutton 排序
if (timeButtonIsDescending)
{//降序-》降序
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_up.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}else
{//升序 --》升序
[self.timeButtonsetImage:[UIImageimageNamed:@"icon_order_down.png"] forState:UIControlStateNormal];
[self.timeButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}
if (priceButtonIsDescending)
{//降序--》升序
priceButtonIsDescending = NO;
[btn setImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[btn setTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}else
{//升序---》降序
priceButtonIsDescending = YES;
[btn setImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[btn setTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}
}else
{//timebutton 排序
if (priceButtonIsDescending)
{//降序-》降序
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_down.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}else
{//升序 --》升序
[self.priceButtonsetImage:[UIImageimageNamed:@"icon_order_up.png"] forState:UIControlStateNormal];
[self.priceButtonsetTitleColor:RGBCOLOR(116, 116, 116) forState:UIControlStateNormal];
}
if (timeButtonIsDescending)
{//降序--》升序
timeButtonIsDescending = NO;
[btn setImage:[UIImageimageNamed:@"icon_order_down_h.png"] forState:UIControlStateNormal];
[btn setTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}else
{//升序---》降序
timeButtonIsDescending = YES;
[btn setImage:[UIImageimageNamed:@"icon_order_up_h.png"] forState:UIControlStateNormal];
[btn setTitleColor:RGBCOLOR(253, 113, 34) forState:UIControlStateNormal];
}
}
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:arr];
self.fListArray = mutableArray;
[self.expansionTableViewreloadData];
}