Sorting ArrayList with sorted Array in Java Ends Up with Different Results The 2019 Stack Overflow Developer Survey Results Are InJava - Sort one array based on values of another array?Create ArrayList from arrayWhat is the difference between public, protected, package-private and private in Java?When to use LinkedList over ArrayList in Java?Sorting an array of JavaScript objects by propertyHow do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueHow do I declare and initialize an array in Java?Sort ArrayList of custom Objects by propertyConvert ArrayList<String> to String[] arrayWhy is it faster to process a sorted array than an unsorted array?

What is the meaning of Triage in Cybersec world?

How come people say “Would of”?

Access elements in std::string where positon of string is greater than its size

What are my rights when I have a Sparpreis ticket but can't board an overcrowded train?

Is bread bad for ducks?

"Riffle" two strings

Springs with some finite mass

Dual Citizen. Exited the US on Italian passport recently

How long do I have to send payment?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

"To split hairs" vs "To be pedantic"

In microwave frequencies, do you use a circulator when you need a (near) perfect diode?

CiviEvent: Public link for events of a specific type

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

Why is it "Tumoren" and not "Tumore"?

How can I create a character who can assume the widest possible range of creature sizes?

Inflated grade on resume at previous job, might former employer tell new employer?

Carnot-Caratheodory metric

What is the steepest angle that a canal can be traversable without locks?

What is this 4-propeller plane?

Is flight data recorder erased after every flight?

How to create dashed lines/arrows in Illustrator

Why is Grand Jury testimony secret?

Why could you hear an Amstrad CPC working?



Sorting ArrayList with sorted Array in Java Ends Up with Different Results



The 2019 Stack Overflow Developer Survey Results Are InJava - Sort one array based on values of another array?Create ArrayList from arrayWhat is the difference between public, protected, package-private and private in Java?When to use LinkedList over ArrayList in Java?Sorting an array of JavaScript objects by propertyHow do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueHow do I declare and initialize an array in Java?Sort ArrayList of custom Objects by propertyConvert ArrayList<String> to String[] arrayWhy 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 an array by sizes of its int numbers(from small to big) and I want to do another arrayList with the same sequence of the array.



In this case, my array is "workHour" and the arrayList is "sortedList" from startHour.



I followed someone's advice from here.



And I wrote my code like below.



int[] workHour = new int[]4,2,6,2,5,4, 4, 3, 4,11, 2;
String[] startHour = new String[] "1","3", "0", "5","3", "5", "6", "8", "8", "2","12";

final List<String> stringListCopy = Arrays.asList(startHour);
ArrayList<String> sortedList = new ArrayList<>(stringListCopy);
Collections.sort(sortedList, (o1, o2) -> workHour[stringListCopy.indexOf(o1)] - workHour[stringListCopy.indexOf(o2)]);

Arrays.sort(workHour);
System.out.println(sortedList);


The reason why I put String instead of Integer into arrayList is because I got the same result, so I switched into String to test my code.



And I got a result like this:




[3, 5, 3, 5, 12, 8, 8, 1, 6, 0, 2]




Not like I expected:




[3, 5, 12, 8, 1, 5, 6, 8, 3, 0, 2]




However when I change the array like this:



String[] startHour = new String[] "c1","a1","e","a2","d","c2","c3","b","c4","f","a3";


It gives me what I expected:




[a1, a2, a3, b, c1, c2, c3, c4, d, e, f]




I simply changed String array with something that I can see the result more easily and I just don't know what makes this difference and why it behaves like this.



I think I need to know about the Comparator class..
Can anyone explain this?










share|improve this question
























  • What is stringAList?

    – Johannes Kuhn
    Mar 22 at 3:06











  • @JohannesKuhn My mistake. I replaced it with stringListCopy.

    – Woogear
    Mar 22 at 3:22

















0















I want to sort an array by sizes of its int numbers(from small to big) and I want to do another arrayList with the same sequence of the array.



In this case, my array is "workHour" and the arrayList is "sortedList" from startHour.



I followed someone's advice from here.



And I wrote my code like below.



int[] workHour = new int[]4,2,6,2,5,4, 4, 3, 4,11, 2;
String[] startHour = new String[] "1","3", "0", "5","3", "5", "6", "8", "8", "2","12";

final List<String> stringListCopy = Arrays.asList(startHour);
ArrayList<String> sortedList = new ArrayList<>(stringListCopy);
Collections.sort(sortedList, (o1, o2) -> workHour[stringListCopy.indexOf(o1)] - workHour[stringListCopy.indexOf(o2)]);

Arrays.sort(workHour);
System.out.println(sortedList);


The reason why I put String instead of Integer into arrayList is because I got the same result, so I switched into String to test my code.



And I got a result like this:




[3, 5, 3, 5, 12, 8, 8, 1, 6, 0, 2]




Not like I expected:




[3, 5, 12, 8, 1, 5, 6, 8, 3, 0, 2]




However when I change the array like this:



String[] startHour = new String[] "c1","a1","e","a2","d","c2","c3","b","c4","f","a3";


It gives me what I expected:




[a1, a2, a3, b, c1, c2, c3, c4, d, e, f]




I simply changed String array with something that I can see the result more easily and I just don't know what makes this difference and why it behaves like this.



I think I need to know about the Comparator class..
Can anyone explain this?










share|improve this question
























  • What is stringAList?

    – Johannes Kuhn
    Mar 22 at 3:06











  • @JohannesKuhn My mistake. I replaced it with stringListCopy.

    – Woogear
    Mar 22 at 3:22













0












0








0








I want to sort an array by sizes of its int numbers(from small to big) and I want to do another arrayList with the same sequence of the array.



In this case, my array is "workHour" and the arrayList is "sortedList" from startHour.



I followed someone's advice from here.



And I wrote my code like below.



int[] workHour = new int[]4,2,6,2,5,4, 4, 3, 4,11, 2;
String[] startHour = new String[] "1","3", "0", "5","3", "5", "6", "8", "8", "2","12";

final List<String> stringListCopy = Arrays.asList(startHour);
ArrayList<String> sortedList = new ArrayList<>(stringListCopy);
Collections.sort(sortedList, (o1, o2) -> workHour[stringListCopy.indexOf(o1)] - workHour[stringListCopy.indexOf(o2)]);

Arrays.sort(workHour);
System.out.println(sortedList);


The reason why I put String instead of Integer into arrayList is because I got the same result, so I switched into String to test my code.



And I got a result like this:




[3, 5, 3, 5, 12, 8, 8, 1, 6, 0, 2]




Not like I expected:




[3, 5, 12, 8, 1, 5, 6, 8, 3, 0, 2]




However when I change the array like this:



String[] startHour = new String[] "c1","a1","e","a2","d","c2","c3","b","c4","f","a3";


It gives me what I expected:




[a1, a2, a3, b, c1, c2, c3, c4, d, e, f]




I simply changed String array with something that I can see the result more easily and I just don't know what makes this difference and why it behaves like this.



I think I need to know about the Comparator class..
Can anyone explain this?










share|improve this question
















I want to sort an array by sizes of its int numbers(from small to big) and I want to do another arrayList with the same sequence of the array.



In this case, my array is "workHour" and the arrayList is "sortedList" from startHour.



I followed someone's advice from here.



And I wrote my code like below.



int[] workHour = new int[]4,2,6,2,5,4, 4, 3, 4,11, 2;
String[] startHour = new String[] "1","3", "0", "5","3", "5", "6", "8", "8", "2","12";

final List<String> stringListCopy = Arrays.asList(startHour);
ArrayList<String> sortedList = new ArrayList<>(stringListCopy);
Collections.sort(sortedList, (o1, o2) -> workHour[stringListCopy.indexOf(o1)] - workHour[stringListCopy.indexOf(o2)]);

Arrays.sort(workHour);
System.out.println(sortedList);


The reason why I put String instead of Integer into arrayList is because I got the same result, so I switched into String to test my code.



And I got a result like this:




[3, 5, 3, 5, 12, 8, 8, 1, 6, 0, 2]




Not like I expected:




[3, 5, 12, 8, 1, 5, 6, 8, 3, 0, 2]




However when I change the array like this:



String[] startHour = new String[] "c1","a1","e","a2","d","c2","c3","b","c4","f","a3";


It gives me what I expected:




[a1, a2, a3, b, c1, c2, c3, c4, d, e, f]




I simply changed String array with something that I can see the result more easily and I just don't know what makes this difference and why it behaves like this.



I think I need to know about the Comparator class..
Can anyone explain this?







java arrays sorting arraylist comparator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 10:27







Woogear

















asked Mar 22 at 2:50









WoogearWoogear

52




52












  • What is stringAList?

    – Johannes Kuhn
    Mar 22 at 3:06











  • @JohannesKuhn My mistake. I replaced it with stringListCopy.

    – Woogear
    Mar 22 at 3:22

















  • What is stringAList?

    – Johannes Kuhn
    Mar 22 at 3:06











  • @JohannesKuhn My mistake. I replaced it with stringListCopy.

    – Woogear
    Mar 22 at 3:22
















What is stringAList?

– Johannes Kuhn
Mar 22 at 3:06





What is stringAList?

– Johannes Kuhn
Mar 22 at 3:06













@JohannesKuhn My mistake. I replaced it with stringListCopy.

– Woogear
Mar 22 at 3:22





@JohannesKuhn My mistake. I replaced it with stringListCopy.

– Woogear
Mar 22 at 3:22












1 Answer
1






active

oldest

votes


















1














It works for this case: "c1","a1","e","a2","d","c2","c3","b","c4","f","a3" because all elements are unique.
indexOf method returns the index of the first occurence of a given element and since your original array i.e. "1","3", "0", "5","3", "5", "6", "8", "8", "2","12" contains duplicates, indexOf is going to return same value for elements "3", "5" and "8" (returns 1 for both"3", 3 for both "5", 7 for both "8")



I don't think you can apply Comparator here since it uses the values of compared elements and your problem requires inspection of indices of compared elements without checking their actual values (well, unless you guarantee that the elements are unique)



Or you may create a list of class Pair with string field having value of strings that your original list consists of, and int field having value of index of corresponding string. In that case you may use comparator to sort the list of pairs and after that you get your sorted list of strings by iterating through the sorted list of pairs






share|improve this answer

























  • Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

    – Woogear
    Mar 22 at 9:16











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%2f55292205%2fsorting-arraylist-with-sorted-array-in-java-ends-up-with-different-results%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














It works for this case: "c1","a1","e","a2","d","c2","c3","b","c4","f","a3" because all elements are unique.
indexOf method returns the index of the first occurence of a given element and since your original array i.e. "1","3", "0", "5","3", "5", "6", "8", "8", "2","12" contains duplicates, indexOf is going to return same value for elements "3", "5" and "8" (returns 1 for both"3", 3 for both "5", 7 for both "8")



I don't think you can apply Comparator here since it uses the values of compared elements and your problem requires inspection of indices of compared elements without checking their actual values (well, unless you guarantee that the elements are unique)



Or you may create a list of class Pair with string field having value of strings that your original list consists of, and int field having value of index of corresponding string. In that case you may use comparator to sort the list of pairs and after that you get your sorted list of strings by iterating through the sorted list of pairs






share|improve this answer

























  • Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

    – Woogear
    Mar 22 at 9:16















1














It works for this case: "c1","a1","e","a2","d","c2","c3","b","c4","f","a3" because all elements are unique.
indexOf method returns the index of the first occurence of a given element and since your original array i.e. "1","3", "0", "5","3", "5", "6", "8", "8", "2","12" contains duplicates, indexOf is going to return same value for elements "3", "5" and "8" (returns 1 for both"3", 3 for both "5", 7 for both "8")



I don't think you can apply Comparator here since it uses the values of compared elements and your problem requires inspection of indices of compared elements without checking their actual values (well, unless you guarantee that the elements are unique)



Or you may create a list of class Pair with string field having value of strings that your original list consists of, and int field having value of index of corresponding string. In that case you may use comparator to sort the list of pairs and after that you get your sorted list of strings by iterating through the sorted list of pairs






share|improve this answer

























  • Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

    – Woogear
    Mar 22 at 9:16













1












1








1







It works for this case: "c1","a1","e","a2","d","c2","c3","b","c4","f","a3" because all elements are unique.
indexOf method returns the index of the first occurence of a given element and since your original array i.e. "1","3", "0", "5","3", "5", "6", "8", "8", "2","12" contains duplicates, indexOf is going to return same value for elements "3", "5" and "8" (returns 1 for both"3", 3 for both "5", 7 for both "8")



I don't think you can apply Comparator here since it uses the values of compared elements and your problem requires inspection of indices of compared elements without checking their actual values (well, unless you guarantee that the elements are unique)



Or you may create a list of class Pair with string field having value of strings that your original list consists of, and int field having value of index of corresponding string. In that case you may use comparator to sort the list of pairs and after that you get your sorted list of strings by iterating through the sorted list of pairs






share|improve this answer















It works for this case: "c1","a1","e","a2","d","c2","c3","b","c4","f","a3" because all elements are unique.
indexOf method returns the index of the first occurence of a given element and since your original array i.e. "1","3", "0", "5","3", "5", "6", "8", "8", "2","12" contains duplicates, indexOf is going to return same value for elements "3", "5" and "8" (returns 1 for both"3", 3 for both "5", 7 for both "8")



I don't think you can apply Comparator here since it uses the values of compared elements and your problem requires inspection of indices of compared elements without checking their actual values (well, unless you guarantee that the elements are unique)



Or you may create a list of class Pair with string field having value of strings that your original list consists of, and int field having value of index of corresponding string. In that case you may use comparator to sort the list of pairs and after that you get your sorted list of strings by iterating through the sorted list of pairs







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 22 at 3:58

























answered Mar 22 at 3:31









mangustamangusta

1,66921329




1,66921329












  • Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

    – Woogear
    Mar 22 at 9:16

















  • Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

    – Woogear
    Mar 22 at 9:16
















Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

– Woogear
Mar 22 at 9:16





Thanks a lot! I realized tried to solve it in wrong way, and need to find other way like yours.

– Woogear
Mar 22 at 9:16



















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%2f55292205%2fsorting-arraylist-with-sorted-array-in-java-ends-up-with-different-results%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

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript