Collection.sort java and comparator
Anonymous User
474
Nov 05, 2020

I was analyzing a piece of code that was the following:

Collections.sort(havefun, new Comparator<Fun>(){

public int compare(Fun hf1, Fun hf2){

    return hf1- hf2;

Can somebody explain what the syntax format of Collections.sort is and what the purpose of the Comparator is. Also, is 'compare' a given function in Java?

I know that Arrays.sort is relatively simple where you just input the array and it sorts the integers but I am a little confused on array lists. What I have deduced from other answers on this platform is that the comparator uses criteria from the compare function to compare the 2 values -- if haveFun was a list of integers, and we were comparing hf1 and hf2, what exactly does hf1 - hf2 accomplish?

Comments (1)