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;
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
|
show 1 more comment
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
3
Yeah but, what's a dictionary sort? And what's the wanted output?
– LppEdd
Mar 23 at 10:01
4
thecompare
method never returns0
. 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
|
show 1 more comment
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
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
java sorting arraylist
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
thecompare
method never returns0
. 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
|
show 1 more comment
3
Yeah but, what's a dictionary sort? And what's the wanted output?
– LppEdd
Mar 23 at 10:01
4
thecompare
method never returns0
. 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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:
- Compare by the first criterion.
- If the given values differ, return the comparison result and be done.
- Take the next criterion, continue with step 1.
- If there are no criteria left, return 0.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- Compare by the first criterion.
- If the given values differ, return the comparison result and be done.
- Take the next criterion, continue with step 1.
- If there are no criteria left, return 0.
add a comment |
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:
- Compare by the first criterion.
- If the given values differ, return the comparison result and be done.
- Take the next criterion, continue with step 1.
- If there are no criteria left, return 0.
add a comment |
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:
- Compare by the first criterion.
- If the given values differ, return the comparison result and be done.
- Take the next criterion, continue with step 1.
- If there are no criteria left, return 0.
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:
- Compare by the first criterion.
- If the given values differ, return the comparison result and be done.
- Take the next criterion, continue with step 1.
- If there are no criteria left, return 0.
edited Mar 23 at 10:30
answered Mar 23 at 10:20
Roland IlligRoland Illig
31.3k96493
31.3k96493
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 returns0
. 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