Unique~sgm

Mysort(选做)

Mysort(选做)

一、题目:

  • 注意:研究sort的其他功能,要能改的动代码,需要答辩
  • 模拟实现Linux下Sort -t : -k 2的功能。
  • 要有伪代码,产品代码,测试代码(注意测试用例的设计)
  • 参考 Sort的实现。提交博客链接。

必须答辩才能得分

  1 import java.util.*;
  2
  3 public class MySort1 {
  4     public static void main(String [] args) {
  5         String [] toSort = {"aaa:10:1:1",
  6                             "ccc:30:3:4",
  7                             "bbb:50:4:5",
  8                             "ddd:20:5:3",
  9                             "eee:40:2:20"};
 10
 11         System.out.println("Before sort:");
 12         for (String str: toSort)
 13                     System.out.println(str);
 14
 15         Arrays.sort(toSort);
 16
 17         System.out.println("After sort:");
 18         for( String str : toSort)
 19             System.out.println(str);
 20     }
 21 }

二、解答:

  • 一些常用选项:
  • -b:忽略每行前面开始出的空格字符;
  • -c:检查文件是否已经按照顺序排序;
  • -d:排序时,处理英文字母、数字及空格字符外,忽略其他的字符;
  • -f:排序时,将小写字母视为大写字母;
  • -i:排序时,除了040至176之间的ASCII字符外,忽略其他的字符;
  • -k:排序时,指定本域的开头和结尾;
  • -m:将几个排序号的文件进行合并;
  • -M:将前面3个字母依照月份的缩写进行排序;
  • -n:依照数值的大小排序;
  • -o<输出文件>:将排序后的结果存入制定的文件;
  • -r:以相反的顺序来排序;
  • -t<分隔字符>:指定排序时所用的栏位分隔字符;
  • Sort -t : -k 2是按照正数第二列的数据大小进行从小到大的排序。
  • public String[] split(String regex):一个String类的数组以regex传入的分隔符为标准,对字符串进行分隔,使用时分隔符要放在双括号中

三、代码:

  • 伪代码:
    • 将数组以数值大小为标准从小到大排列
    • 新建数组a,长度与tosort数据长度相同
    • 调用split函数将数组以(冒号)为分隔符分成小字符串
    • 调用sort函数进行排序
    • 使用嵌套循环输出排序后的结果
  • 代码:
import java.util.*;

public class MySort {
    public static void main(String[] args) {
        String[] toSort = {"aaa:10:1:1",
                "ccc:30:3:4",
                "bbb:50:4:5",
                "ddd:20:5:3",
                "eee:40:2:20"};

        System.out.println("Before sort:");
        for (String str : toSort)
            System.out.println(str);
            System.out.println("After sort:");
        int[] s = new int[toSort.length];
        String[][] string = new String [toSort.length][4];
        for (int i = 0; i < toSort.length; i++) {
            string[i] = toSort[i].split(":");
            s[i] = Integer.parseInt(string[i][1]);
        }
        Arrays.sort(s);
        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < toSort.length; j++) {
                if(s[i] == Integer.parseInt(string[j][1])){
                    System.out.println(toSort[j]);
                }
            }
        }
    }
}

四、结果截图:

五、码云链接:
https://gitee.com/sgm5/text1/commit/c0093a4a4852e5d7520e733fbd3ad3be272b14b8

posted on 2019-05-19 09:55  胖铭鸭  阅读(210)  评论(0编辑  收藏  举报

导航