按照XX数组排序

 

    Student *stu1 = [Student studentWithname:@"丽丽" age:13 score:55];

    Student *stu2 = [Student studentWithname:@"炎炎" age:14 score:57];

    Student *stu3 = [Student studentWithname:@"娜娜" age:12 score:46];

    Student *stu4 = [Student studentWithname:@"翩翩" age:11 score:56];

    NSArray *stus = [NSArray arrayWithObjects:stu1 , stu2, stu3, stu4, nil];

    NSComparisonResult (^scoreComparator)(id, id) = ^(id s1, id s2){

        Student *stua = (Student *)s1;

        Student *stub = (Student *)s2;

        float result = [stua score] - [stub score];

        NSComparisonResult r ;

        if (result < 0) {

            r = NSOrderedAscending;

        } else if(result > 0){

            r = NSOrderedDescending;

        } else {

       

            r = NSOrderedSame;

        }

        return r;

    };

    NSArray *sortArray = [stus sortedArrayUsingComparator:scoreComparator];

    for (Student *stu in sortArray) {

        NSLog(@"%@ : %.2f", [stu name], [stu score]);

    }

   

    NSComparisonResult (^nameComparetor)(id, id) = ^(id s1, id s2){

        Student *st1 = (Student *)s1;

        Student *st2 = (Student *)s2;

        return [[st2 name] compare:[st1 name] ];

       

    };

    NSArray *nameArray = [stus sortedArrayUsingComparator:nameComparetor];

    for (Student *s in nameArray) {

        NSLog(@"%@ :%.2f", [s name], [s score]);

    }

   

   

    NSComparisonResult (^ageComparator)(id, id) = ^(id s1, id s2){

   

        Student *sr1 = (Student *)s1;

        Student *sr2 = (Student *)s2;

        long result = [sr1 age] - [sr2 age];

        NSComparisonResult e;

        if (result < 0) {

            e = NSOrderedAscending;

        } else if (result > 0){

            e = NSOrderedDescending;

        } else {

            e = NSOrderedSame;

        }

        return e;

    };

   

    NSArray *ageArray = [stus sortedArrayUsingComparator:ageComparator];

    for (Student *sw in ageArray) {

        NSLog(@"%@ : %ld", [sw name], [sw age]);

    }

   

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Java中,可以使用Collections.sort()方法对日期进行排序。需要先实现Comparable接口并重写compareTo()方法,在compareTo()方法中按照日期进行比较。 例如,类Date中有月和日的属性,可以这样实现compareTo()方法: ``` public class Date implements Comparable<Date> { int month; int day; public int compareTo(Date other) { if (month != other.month) { return month - other.month; } return day - other.day; } } ``` 然后可以使用 Collections.sort(list) 排序。 另外,也可以使用Arrays.sort()方法和java.util.Comparator接口进行排序。 例如: ``` Arrays.sort(dateArray, new Comparator<Date>() { public int compare(Date d1, Date d2) { if (d1.month != d2.month) { return d1.month - d2.month; } return d1.day - d2.day; } }); ``` 还可以使用java8的lambda表达式,简化代码 ``` Arrays.sort(dateArray, (d1, d2) -> { if (d1.month != d2.month) { return d1.month - d2.month; } return d1.day - d2.day; }); ``` ### 回答2: 以下是使用Java对某个月某天的数据进行排序的示例代码: ```java import java.util.Arrays; public class DataSorting { public static void main(String[] args) { // 假设有一个存储数据的数组 int[] data = {10, 5, 8, 3, 6, 12, 9}; // 输出排序前的数据 System.out.println("排序前的数据:"); for (int num : data) { System.out.print(num + " "); } System.out.println(); // 使用Java的Arrays类对数据进行排序 Arrays.sort(data); // 输出排序后的数据 System.out.println("排序后的数据:"); for (int num : data) { System.out.print(num + " "); } } } ``` 这段代码首先定义了一个数组`data`,内部存储了一组整数数据。然后通过调用`Arrays.sort(data)`对数据进行排序排序后再通过遍历数组输出排序后的数据。 要使这段代码适用于不同的日期数据,只需将需要排序的数据存储在`data`数组中即可。可以根据实际情况修改`data`数组中的元素,并调整数组大小。 注意:代码中的`import`语句用于导入`java.util.Arrays`类,以便可以使用`Arrays.sort()`方法。 ### 回答3: 要对xxxx日的数据进行排序,可以使用Java语言来实现。下面是一个示例代码: ```java import java.util.Arrays; public class DataSort { public static void main(String[] args) { // 假设有一个int类型的数组data,表示某一天的数据 int[] data = {5, 2, 8, 1, 3}; // 这里只是示例数据,实际情况中需要替换成具体的数据 // 使用Arrays工具类的sort方法将数组进行排序 Arrays.sort(data); // 打印排序后的结果 System.out.println("排序后的数据:"); for (int num : data) { System.out.print(num + " "); } } } ``` 以上代码中,我们使用了`Arrays.sort()`方法来对数组进行排序,该方法会按照升序排列数组元素。你可以将`int[] data`数组替换成具体的数据,然后运行代码,就能得到排序后的结果。 需要注意的是,这只是一个示例代码,具体的情况中可能需要使用其他类型的数据,比如字符串、日期等。根据具体需要选择合适的数据类型和排序方法来实现对数据的排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值