如何根据状态后跟另一个字段对数组进行自定义排序?

时间:2016-12-27 12:13:10

标签: arrays sorting

所以我有一个数组,我需要根据状态按以下顺序对其进行排序,即失败 - > warn->完成。 通常,我使用lodash进行排序,但这里有一些复杂的顺序。在运行排序后,还需要根据运行进行排序。我不知道如何开始这个。

var arr = [{"status":"failure","name":"one","run":1},
{"status":"failure","name":"two","run":2},
{"status":"warn","name":"three","run":1},
{"status":"warn","name":"four","run":2},
{"status":"completed","name":"five","run":1}]

2 个答案:

答案 0 :(得分:0)

这样的事情?

  1. 为您的排序键添加额外的属性。
  2. var arrnew =   arr.map(function(item){
        switch(item.status){
        case "failure": 
        item.statusId=1;
        break;
        case "warn": 
        item.statusId=2;}
        //...
         return item} );
    
    1. 使用
    2. 排序
        

      _。sortBy(arrnew,' statusId');

答案 1 :(得分:0)

根据“状态”进行排序,然后通过“运行”1或2进行子切片:

 protected String doInBackground(String... f_url) {
        try {
            long total = 0;
            URL url = new URL(f_url[0]);
            HttpURLConnection conection = (HttpURLConnection) url.openConnection();
            int lenghtOfFile = conection.getContentLength();
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
            conection.connect();
            BufferedInputStream input = new BufferedInputStream(conection.getInputStream());

            byte data[] = new byte[8192];
            int lastcount = 0;


            while ((count = input.read(data)) != -1) {

                if (isCanceled) { // this code waiting the click button :)
                    file.delete();
                    downloadresult = false;
                    break;
                }
                if (intCheck()) { // check internet and download
                    total += count;
                    downloadresult = true;
                    int ProgBarCount = (int) ((total * 100) / lenghtOfFile);
                    if (ProgBarCount > lastcount) {
                        lastcount = ProgBarCount;
                        publishProgress(Integer.toString(ProgBarCount));
                    }
                    output.write(data, 0, count);

                }
                else {

                     Thread.sleep(4000); //doesn't work, doesn't sleep
                     downloadresult = false;
                }

            }

            output.flush();
            output.close();
            input.close();
        }
        catch (Exception e) {

            e.printStackTrace();
         exmessage = e.getMessage().toString();
            downloadresult = false;

        }
        return null;
    }