Dictionary sorting of ArrayList of Arraylist in javaIs Java “pass-by-reference” or “pass-by-value”?How do I sort a list of dictionaries by a value of the dictionary?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I sort a dictionary by value?Initialization of an ArrayList in one lineSort array of objects by string property valueSort ArrayList of custom Objects by propertyWhy is it faster to process a sorted array than an unsorted array?

Why do Thanos' punches not kill Captain America or at least cause some mortal injuries?

A Cunning Riley Riddle

Why does increasing the sampling rate make implementing an anti-aliasing filter easier?

Improving Sati-Sampajañña (situative wisdom)

What's the "magic similar to the Knock spell" referenced in the Dungeon of the Mad Mage adventure?

Should I pay on student loans in deferment or continue to snowball other debts?

How to evaluate sum with one million summands?

How is CoreiX like Corei5, i7 is related to Haswell, Ivy Bridge?

date to display the EDT time

Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?

Windows OS quantum vs. SQL OS Quantum

Remove color cast in darktable?

Why does a variable size struct not compile in the Arduino IDE?

No such column 'DeveloperName' on entity 'RecordType' after Summer '19 release on sandbox

Is it a Munchausen Number?

Watching the game, having a puzzle

Why is PerfectForwardSecrecy considered OK, when it has same defects as salt-less password hashing?

Are there variations of the regular runtimes of the Big-O-Notation?

The meaning of a て-form verb at the end of this sentence

What can cause an unfrozen indoor copper drain pipe to crack?

How are one-time password generators like Google Authenticator different from having two passwords?

Is every story set in the future "science fiction"?

Company stopped paying my salary. What are my options?

Was there a contingency plan in place if Little Boy failed to detonate?



Dictionary sorting of ArrayList of Arraylist in java


Is Java “pass-by-reference” or “pass-by-value”?How do I sort a list of dictionaries by a value of the dictionary?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I sort a dictionary by value?Initialization of an ArrayList in one lineSort array of objects by string property valueSort ArrayList of custom Objects by propertyWhy is it faster to process a sorted array than an unsorted array?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to sort the ArrayList of ArrayList<Integer>, which looks like.



ArrayList<ArrayList<Integer>> allres = new ArrayList<ArrayList<Integer>>();
Collections.sort(allres, new Comparator<ArrayList<Integer>>()
public int compare(ArrayList<Integer> a, ArrayList<Integer> b)
for (int i = 0; i < a.size(); i++)
if (a.get(i) < b.get(i))
return -1;
else if (a.get(i) == b.get(i))
continue;
else
return 1;


return -1;

);


But for some input, it is not giving the result of dictionary sort.










share|improve this question



















  • 3





    Yeah but, what's a dictionary sort? And what's the wanted output?

    – LppEdd
    Mar 23 at 10:01






  • 4





    the compare method never returns 0. what if all items are equal?

    – Sharon Ben Asher
    Mar 23 at 10:06






  • 1





    The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

    – bambula
    Mar 23 at 10:13







  • 1





    @bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

    – Roland Illig
    Mar 23 at 10:24






  • 1





    And what is "some input". Don't make us guess, provide us with details instead.

    – Roland Illig
    Mar 23 at 10:27

















0















I want to sort the ArrayList of ArrayList<Integer>, which looks like.



ArrayList<ArrayList<Integer>> allres = new ArrayList<ArrayList<Integer>>();
Collections.sort(allres, new Comparator<ArrayList<Integer>>()
public int compare(ArrayList<Integer> a, ArrayList<Integer> b)
for (int i = 0; i < a.size(); i++)
if (a.get(i) < b.get(i))
return -1;
else if (a.get(i) == b.get(i))
continue;
else
return 1;


return -1;

);


But for some input, it is not giving the result of dictionary sort.










share|improve this question



















  • 3





    Yeah but, what's a dictionary sort? And what's the wanted output?

    – LppEdd
    Mar 23 at 10:01






  • 4





    the compare method never returns 0. what if all items are equal?

    – Sharon Ben Asher
    Mar 23 at 10:06






  • 1





    The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

    – bambula
    Mar 23 at 10:13







  • 1





    @bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

    – Roland Illig
    Mar 23 at 10:24






  • 1





    And what is "some input". Don't make us guess, provide us with details instead.

    – Roland Illig
    Mar 23 at 10:27













0












0








0


1






I want to sort the ArrayList of ArrayList<Integer>, which looks like.



ArrayList<ArrayList<Integer>> allres = new ArrayList<ArrayList<Integer>>();
Collections.sort(allres, new Comparator<ArrayList<Integer>>()
public int compare(ArrayList<Integer> a, ArrayList<Integer> b)
for (int i = 0; i < a.size(); i++)
if (a.get(i) < b.get(i))
return -1;
else if (a.get(i) == b.get(i))
continue;
else
return 1;


return -1;

);


But for some input, it is not giving the result of dictionary sort.










share|improve this question
















I want to sort the ArrayList of ArrayList<Integer>, which looks like.



ArrayList<ArrayList<Integer>> allres = new ArrayList<ArrayList<Integer>>();
Collections.sort(allres, new Comparator<ArrayList<Integer>>()
public int compare(ArrayList<Integer> a, ArrayList<Integer> b)
for (int i = 0; i < a.size(); i++)
if (a.get(i) < b.get(i))
return -1;
else if (a.get(i) == b.get(i))
continue;
else
return 1;


return -1;

);


But for some input, it is not giving the result of dictionary sort.







java sorting arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 12:06









Pshemo

96.9k15135194




96.9k15135194










asked Mar 23 at 9:57









Sujeet_JavaDevSujeet_JavaDev

61




61







  • 3





    Yeah but, what's a dictionary sort? And what's the wanted output?

    – LppEdd
    Mar 23 at 10:01






  • 4





    the compare method never returns 0. what if all items are equal?

    – Sharon Ben Asher
    Mar 23 at 10:06






  • 1





    The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

    – bambula
    Mar 23 at 10:13







  • 1





    @bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

    – Roland Illig
    Mar 23 at 10:24






  • 1





    And what is "some input". Don't make us guess, provide us with details instead.

    – Roland Illig
    Mar 23 at 10:27












  • 3





    Yeah but, what's a dictionary sort? And what's the wanted output?

    – LppEdd
    Mar 23 at 10:01






  • 4





    the compare method never returns 0. what if all items are equal?

    – Sharon Ben Asher
    Mar 23 at 10:06






  • 1





    The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

    – bambula
    Mar 23 at 10:13







  • 1





    @bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

    – Roland Illig
    Mar 23 at 10:24






  • 1





    And what is "some input". Don't make us guess, provide us with details instead.

    – Roland Illig
    Mar 23 at 10:27







3




3





Yeah but, what's a dictionary sort? And what's the wanted output?

– LppEdd
Mar 23 at 10:01





Yeah but, what's a dictionary sort? And what's the wanted output?

– LppEdd
Mar 23 at 10:01




4




4





the compare method never returns 0. what if all items are equal?

– Sharon Ben Asher
Mar 23 at 10:06





the compare method never returns 0. what if all items are equal?

– Sharon Ben Asher
Mar 23 at 10:06




1




1





The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

– bambula
Mar 23 at 10:13






The comparator is kinda weird. It compares two lists of integers by first found difference (ie list [1,6,2,3,8] would be smaller than [1,6,3,0,1]) because the the first difference is at index 2 and number 2 is smaller than 3 the rest is ignored. I don't know what is the meaning of such a comparison. There's possible IndexOutOfBoundsException if the first list is longer than the second and the items are equal. Another weird behaviour: the comparator consider first list "smaller" (returns -1) than the second in case they are the same...

– bambula
Mar 23 at 10:13





1




1





@bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

– Roland Illig
Mar 23 at 10:24





@bambula This kind of comparators makes sense for version numbers and several other things. The concept is sound, it's just the implementation that's broken.

– Roland Illig
Mar 23 at 10:24




1




1





And what is "some input". Don't make us guess, provide us with details instead.

– Roland Illig
Mar 23 at 10:27





And what is "some input". Don't make us guess, provide us with details instead.

– Roland Illig
Mar 23 at 10:27












1 Answer
1






active

oldest

votes


















1














Comparator functions that contain 1 or -1 are error prone.



Instead, you should write it like this:



public static int lexicographically(List<Integer> a, List<Integer> b) 
for (int i = 0, end = Math.min(a.size(), b.size()); i < end; i++)
int res = Integer.compare(a.get(i), b.get(i));
if (res != 0)
return res;

return Integer.compare(a.size(), b.size());



This style of writing comparator functions prevents various common mistakes, such as never returning 0.



The most important pattern in this style is:



  1. Compare by the first criterion.

  2. If the given values differ, return the comparison result and be done.

  3. Take the next criterion, continue with step 1.

  4. If there are no criteria left, return 0.





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312543%2fdictionary-sorting-of-arraylist-of-arraylistinteger-in-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Comparator functions that contain 1 or -1 are error prone.



    Instead, you should write it like this:



    public static int lexicographically(List<Integer> a, List<Integer> b) 
    for (int i = 0, end = Math.min(a.size(), b.size()); i < end; i++)
    int res = Integer.compare(a.get(i), b.get(i));
    if (res != 0)
    return res;

    return Integer.compare(a.size(), b.size());



    This style of writing comparator functions prevents various common mistakes, such as never returning 0.



    The most important pattern in this style is:



    1. Compare by the first criterion.

    2. If the given values differ, return the comparison result and be done.

    3. Take the next criterion, continue with step 1.

    4. If there are no criteria left, return 0.





    share|improve this answer





























      1














      Comparator functions that contain 1 or -1 are error prone.



      Instead, you should write it like this:



      public static int lexicographically(List<Integer> a, List<Integer> b) 
      for (int i = 0, end = Math.min(a.size(), b.size()); i < end; i++)
      int res = Integer.compare(a.get(i), b.get(i));
      if (res != 0)
      return res;

      return Integer.compare(a.size(), b.size());



      This style of writing comparator functions prevents various common mistakes, such as never returning 0.



      The most important pattern in this style is:



      1. Compare by the first criterion.

      2. If the given values differ, return the comparison result and be done.

      3. Take the next criterion, continue with step 1.

      4. If there are no criteria left, return 0.





      share|improve this answer



























        1












        1








        1







        Comparator functions that contain 1 or -1 are error prone.



        Instead, you should write it like this:



        public static int lexicographically(List<Integer> a, List<Integer> b) 
        for (int i = 0, end = Math.min(a.size(), b.size()); i < end; i++)
        int res = Integer.compare(a.get(i), b.get(i));
        if (res != 0)
        return res;

        return Integer.compare(a.size(), b.size());



        This style of writing comparator functions prevents various common mistakes, such as never returning 0.



        The most important pattern in this style is:



        1. Compare by the first criterion.

        2. If the given values differ, return the comparison result and be done.

        3. Take the next criterion, continue with step 1.

        4. If there are no criteria left, return 0.





        share|improve this answer















        Comparator functions that contain 1 or -1 are error prone.



        Instead, you should write it like this:



        public static int lexicographically(List<Integer> a, List<Integer> b) 
        for (int i = 0, end = Math.min(a.size(), b.size()); i < end; i++)
        int res = Integer.compare(a.get(i), b.get(i));
        if (res != 0)
        return res;

        return Integer.compare(a.size(), b.size());



        This style of writing comparator functions prevents various common mistakes, such as never returning 0.



        The most important pattern in this style is:



        1. Compare by the first criterion.

        2. If the given values differ, return the comparison result and be done.

        3. Take the next criterion, continue with step 1.

        4. If there are no criteria left, return 0.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 10:30

























        answered Mar 23 at 10:20









        Roland IlligRoland Illig

        31.3k96493




        31.3k96493





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312543%2fdictionary-sorting-of-arraylist-of-arraylistinteger-in-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현