开发者

How to sort an ArrayList<String> based on specific index range

开发者 https://www.devze.com 2023-03-31 17:46 出处:网络
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 I want to sort this list from index 1 t

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 I want to sort this list from index 1 till last index.

One way As I think that I can c开发者_StackOverflow社区reate a sub-list from main list with desired index range, sort it and add the sub-list accordingly. But my question is:

Is there any API already available for that? Or any other faster way to achieve this.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消