How to sort an ArrayList based on specific index range

前端 未结 2 1663
滥情空心
滥情空心 2021-01-18 07:13

My need is to sort the ArrayList of Strings, based on specific index range. For example I have following items in list: [\"abc\", \"xyz\", \"pqr\" , \"asd\"]Now

相关标签:
2条回答
  • 2021-01-18 07:45

    You should do

    Collections.sort(yourList.subList(1, yourList.size()));
    

    Since the List.subList method returns a view of the list, modifications done by Collections.sort will affect the backing list as well.

    0 讨论(0)
  • 2021-01-18 07:50

    DetailsVOTemp Model have Time and Add set-get method and I have already one list which is a list with the name of list Get the time and lat long from the list and store in tow ArrayList than finally sort it

        List< DetailsVOTemp> firstlist = new ArrayList<>();
        List<DetailsVOTemp> secondlisr = new ArrayList<>();
    
        for (int i = 0; i < list.size(); i++) {
             DetailsVOTemp mDetailsVOTemp = new  DetailsVOTemp();
            mDetailsVOTemp.setTime(list.get(i).getTime());
            mDetailsVOTemp.setAdd(list.get(i).getLatLong());
            mVoTemps.add(mDetailsVOTemp);
        }
        for (int i = 0; i < list.size(); i++) {
            DetailsVOTemp mDetailsVOTemp = new  DetailsVOTemp();
            mDetailsVOTemp.setTime(list.get(i).getDropOffTime());
            mDetailsVOTemp.setAdd(list.get(i).getDropLatLong());
            mVoTemps1.add(mDetailsVOTemp);
        }
    
        mVoTemps.addAll(mVoTemps1);
        Collections.sort(mVoTemps, new Comparator<DetailsVOTemp>() {
    
            @Override
            public int compare(DetailsVOTemp o1, DetailsVOTemp o2) {
                return o1.getTime().compareTo(o2.getTime());
            }
        });
    
        StringBuilder mBuilder = new StringBuilder();
        StringBuilder mBuilder1 = new StringBuilder();
        for (int i = 0; i < mVoTemps.size(); i++) {
            mBuilder1.append(mVoTemps.get(i).getTime() +" :: ");
            mBuilder.append(mVoTemps.get(i).getAdd() +" :: ");
        }
    
        Log.d("Time : ", mBuilder1.toString());
        Log.d("Address : ", mBuilder.toString());
    
    0 讨论(0)
提交回复
热议问题