Sorting array with multiple elements in it by ascending order [duplicate]





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







-3
















This question already has an answer here:




  • How to sort an array of objects in Java?

    9 answers




I am also looking for potential ways that I can incorporate a for loop.



I am learning Java on my own and am terribly confused on how I can sort the below array by type and then by price. This question is not similar to one that has been previously posted, because the question flagged only involves Strings, while mine uses a combination of ints, Strings, and doubles. All of the past posts I have looked at on Overflow before making my own post have not involved doubles in any way.



This is what I have defined Item as.



public Item(int type, String name, double quantity, double price) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.price = price;
}


This is my array:



public static void main ()
{Item shoppingItems = {new Item(2,"Cherry",9,1.35),
new Item(3,"Orange Juice",4,5.29),
new Item(5,"Hand Soap",2,1.77),
new Item(6,"Tooth Brush",3,4.55),
new Item(4,"Cupcake",3,2.95),
new Item(1,"Red Tomato Sauce",5.5,2.35),
new Item(3,"Chicken",1.9,2.48),
new Item(3,"Apple Pie",2,3.99),
new Item(7,"Bug Spray",1,9.28),
new Item(3,"Roast Beef",2.82,5.99),
new Item(5,"Light Bulb",3,3.92),
new Item(4,"Cookies",0.2,2.96),
new Item(2,"Watermelon",1.8,2.29)
};
}


How can I sort this array in ascending order by type? And also by price?



I looked into using Comparators, but they did not seem to work for my objective. I'm also not sure because price is a double.










share|improve this question















marked as duplicate by talex, dave, Naman java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 3:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • you wan to sort by type and by price? which comes first ?

    – mkjh
    Jan 4 at 2:58











  • @talex That post doesn't teach me how to use Comparator with double, only ints and Strings

    – Math is Life
    Jan 4 at 3:07











  • @MathisLife with doubles all exactly same.

    – talex
    Jan 4 at 3:08











  • @mkjh type comes first

    – Math is Life
    Jan 4 at 3:26


















-3
















This question already has an answer here:




  • How to sort an array of objects in Java?

    9 answers




I am also looking for potential ways that I can incorporate a for loop.



I am learning Java on my own and am terribly confused on how I can sort the below array by type and then by price. This question is not similar to one that has been previously posted, because the question flagged only involves Strings, while mine uses a combination of ints, Strings, and doubles. All of the past posts I have looked at on Overflow before making my own post have not involved doubles in any way.



This is what I have defined Item as.



public Item(int type, String name, double quantity, double price) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.price = price;
}


This is my array:



public static void main ()
{Item shoppingItems = {new Item(2,"Cherry",9,1.35),
new Item(3,"Orange Juice",4,5.29),
new Item(5,"Hand Soap",2,1.77),
new Item(6,"Tooth Brush",3,4.55),
new Item(4,"Cupcake",3,2.95),
new Item(1,"Red Tomato Sauce",5.5,2.35),
new Item(3,"Chicken",1.9,2.48),
new Item(3,"Apple Pie",2,3.99),
new Item(7,"Bug Spray",1,9.28),
new Item(3,"Roast Beef",2.82,5.99),
new Item(5,"Light Bulb",3,3.92),
new Item(4,"Cookies",0.2,2.96),
new Item(2,"Watermelon",1.8,2.29)
};
}


How can I sort this array in ascending order by type? And also by price?



I looked into using Comparators, but they did not seem to work for my objective. I'm also not sure because price is a double.










share|improve this question















marked as duplicate by talex, dave, Naman java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 3:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • you wan to sort by type and by price? which comes first ?

    – mkjh
    Jan 4 at 2:58











  • @talex That post doesn't teach me how to use Comparator with double, only ints and Strings

    – Math is Life
    Jan 4 at 3:07











  • @MathisLife with doubles all exactly same.

    – talex
    Jan 4 at 3:08











  • @mkjh type comes first

    – Math is Life
    Jan 4 at 3:26














-3












-3








-3









This question already has an answer here:




  • How to sort an array of objects in Java?

    9 answers




I am also looking for potential ways that I can incorporate a for loop.



I am learning Java on my own and am terribly confused on how I can sort the below array by type and then by price. This question is not similar to one that has been previously posted, because the question flagged only involves Strings, while mine uses a combination of ints, Strings, and doubles. All of the past posts I have looked at on Overflow before making my own post have not involved doubles in any way.



This is what I have defined Item as.



public Item(int type, String name, double quantity, double price) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.price = price;
}


This is my array:



public static void main ()
{Item shoppingItems = {new Item(2,"Cherry",9,1.35),
new Item(3,"Orange Juice",4,5.29),
new Item(5,"Hand Soap",2,1.77),
new Item(6,"Tooth Brush",3,4.55),
new Item(4,"Cupcake",3,2.95),
new Item(1,"Red Tomato Sauce",5.5,2.35),
new Item(3,"Chicken",1.9,2.48),
new Item(3,"Apple Pie",2,3.99),
new Item(7,"Bug Spray",1,9.28),
new Item(3,"Roast Beef",2.82,5.99),
new Item(5,"Light Bulb",3,3.92),
new Item(4,"Cookies",0.2,2.96),
new Item(2,"Watermelon",1.8,2.29)
};
}


How can I sort this array in ascending order by type? And also by price?



I looked into using Comparators, but they did not seem to work for my objective. I'm also not sure because price is a double.










share|improve this question

















This question already has an answer here:




  • How to sort an array of objects in Java?

    9 answers




I am also looking for potential ways that I can incorporate a for loop.



I am learning Java on my own and am terribly confused on how I can sort the below array by type and then by price. This question is not similar to one that has been previously posted, because the question flagged only involves Strings, while mine uses a combination of ints, Strings, and doubles. All of the past posts I have looked at on Overflow before making my own post have not involved doubles in any way.



This is what I have defined Item as.



public Item(int type, String name, double quantity, double price) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.price = price;
}


This is my array:



public static void main ()
{Item shoppingItems = {new Item(2,"Cherry",9,1.35),
new Item(3,"Orange Juice",4,5.29),
new Item(5,"Hand Soap",2,1.77),
new Item(6,"Tooth Brush",3,4.55),
new Item(4,"Cupcake",3,2.95),
new Item(1,"Red Tomato Sauce",5.5,2.35),
new Item(3,"Chicken",1.9,2.48),
new Item(3,"Apple Pie",2,3.99),
new Item(7,"Bug Spray",1,9.28),
new Item(3,"Roast Beef",2.82,5.99),
new Item(5,"Light Bulb",3,3.92),
new Item(4,"Cookies",0.2,2.96),
new Item(2,"Watermelon",1.8,2.29)
};
}


How can I sort this array in ascending order by type? And also by price?



I looked into using Comparators, but they did not seem to work for my objective. I'm also not sure because price is a double.





This question already has an answer here:




  • How to sort an array of objects in Java?

    9 answers








java arrays sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 6:01







Math is Life

















asked Jan 4 at 2:54









Math is LifeMath is Life

1003




1003




marked as duplicate by talex, dave, Naman java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 3:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by talex, dave, Naman java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 3:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • you wan to sort by type and by price? which comes first ?

    – mkjh
    Jan 4 at 2:58











  • @talex That post doesn't teach me how to use Comparator with double, only ints and Strings

    – Math is Life
    Jan 4 at 3:07











  • @MathisLife with doubles all exactly same.

    – talex
    Jan 4 at 3:08











  • @mkjh type comes first

    – Math is Life
    Jan 4 at 3:26



















  • you wan to sort by type and by price? which comes first ?

    – mkjh
    Jan 4 at 2:58











  • @talex That post doesn't teach me how to use Comparator with double, only ints and Strings

    – Math is Life
    Jan 4 at 3:07











  • @MathisLife with doubles all exactly same.

    – talex
    Jan 4 at 3:08











  • @mkjh type comes first

    – Math is Life
    Jan 4 at 3:26

















you wan to sort by type and by price? which comes first ?

– mkjh
Jan 4 at 2:58





you wan to sort by type and by price? which comes first ?

– mkjh
Jan 4 at 2:58













@talex That post doesn't teach me how to use Comparator with double, only ints and Strings

– Math is Life
Jan 4 at 3:07





@talex That post doesn't teach me how to use Comparator with double, only ints and Strings

– Math is Life
Jan 4 at 3:07













@MathisLife with doubles all exactly same.

– talex
Jan 4 at 3:08





@MathisLife with doubles all exactly same.

– talex
Jan 4 at 3:08













@mkjh type comes first

– Math is Life
Jan 4 at 3:26





@mkjh type comes first

– Math is Life
Jan 4 at 3:26












4 Answers
4






active

oldest

votes


















2














You can do this using Collections.sort method. Just need to pass a custom Comparator implementation as below.



    List<Item> list = Arrays.asList(shoppingItems);
Collections.sort(list, new Comparator<Item>() {
@Override
public int compare(Item item1, Item item2) {
int typeCompareResult = Integer.compare(item1.type, item2.type);
if (typeCompareResult != 0) {
return typeCompareResult;
} else {
return Double.compare(item1.price, item2.price);
}
}
});


EDIT: This is old school way of doing things. For start this is good, but ultimately take advantage of Comparator.comparingInt added in Java 8 which is more concise. Refer KaNa0011's answer



Check Object ordering for more clarity.






share|improve this answer


























  • Do I need anything else before or after this code? What sort of imports should I add?

    – Math is Life
    Jan 4 at 3:27











  • How do I solve a NullPointerException error?

    – Math is Life
    Jan 4 at 3:31











  • My bad, it's working now

    – shriyog
    Jan 4 at 3:33











  • Is there a way to do this using a for loop for price?

    – Math is Life
    Jan 4 at 5:41



















1














You can sort them by using a custom comparator object created by chaining Comparator.comparing(...) calls.



Comparator<Item> itemComparator = Comparator
.comparingInt(Item::getType)
.thenComparingDouble(Item::getPrice);

Arrays.sort(soppingItems, itemComparator);





share|improve this answer
























  • What's this class Comparator package references would be helpful

    – shriyog
    Jan 4 at 3:36






  • 1





    Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

    – KaNa0011
    Jan 4 at 3:41











  • So, it's the same old java.util.Comparator with added methods in Java 8 release

    – shriyog
    Jan 4 at 3:41











  • Is there a way to do this using a for loop?

    – Math is Life
    Jan 4 at 5:20











  • There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

    – KaNa0011
    Jan 4 at 6:28



















0














    List<Item> list = Arrays.asList(shoppingItems);
Collections.sort(list,Comparator.comparingInt(Item::getType));
System.out.println(list.toString());


Assume you have the corresponding getType() and toString() defined for your Item class






share|improve this answer































    -1














    The Collections.sort() should be able to help you.



    Please have a look at this article (Sorting an ArrayList according to user defined criteria.):
    https://www.geeksforgeeks.org/collections-sort-java-examples/






    share|improve this answer



















    • 1





      Please avoid link only answers.

      – talex
      Jan 4 at 2:59


















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can do this using Collections.sort method. Just need to pass a custom Comparator implementation as below.



        List<Item> list = Arrays.asList(shoppingItems);
    Collections.sort(list, new Comparator<Item>() {
    @Override
    public int compare(Item item1, Item item2) {
    int typeCompareResult = Integer.compare(item1.type, item2.type);
    if (typeCompareResult != 0) {
    return typeCompareResult;
    } else {
    return Double.compare(item1.price, item2.price);
    }
    }
    });


    EDIT: This is old school way of doing things. For start this is good, but ultimately take advantage of Comparator.comparingInt added in Java 8 which is more concise. Refer KaNa0011's answer



    Check Object ordering for more clarity.






    share|improve this answer


























    • Do I need anything else before or after this code? What sort of imports should I add?

      – Math is Life
      Jan 4 at 3:27











    • How do I solve a NullPointerException error?

      – Math is Life
      Jan 4 at 3:31











    • My bad, it's working now

      – shriyog
      Jan 4 at 3:33











    • Is there a way to do this using a for loop for price?

      – Math is Life
      Jan 4 at 5:41
















    2














    You can do this using Collections.sort method. Just need to pass a custom Comparator implementation as below.



        List<Item> list = Arrays.asList(shoppingItems);
    Collections.sort(list, new Comparator<Item>() {
    @Override
    public int compare(Item item1, Item item2) {
    int typeCompareResult = Integer.compare(item1.type, item2.type);
    if (typeCompareResult != 0) {
    return typeCompareResult;
    } else {
    return Double.compare(item1.price, item2.price);
    }
    }
    });


    EDIT: This is old school way of doing things. For start this is good, but ultimately take advantage of Comparator.comparingInt added in Java 8 which is more concise. Refer KaNa0011's answer



    Check Object ordering for more clarity.






    share|improve this answer


























    • Do I need anything else before or after this code? What sort of imports should I add?

      – Math is Life
      Jan 4 at 3:27











    • How do I solve a NullPointerException error?

      – Math is Life
      Jan 4 at 3:31











    • My bad, it's working now

      – shriyog
      Jan 4 at 3:33











    • Is there a way to do this using a for loop for price?

      – Math is Life
      Jan 4 at 5:41














    2












    2








    2







    You can do this using Collections.sort method. Just need to pass a custom Comparator implementation as below.



        List<Item> list = Arrays.asList(shoppingItems);
    Collections.sort(list, new Comparator<Item>() {
    @Override
    public int compare(Item item1, Item item2) {
    int typeCompareResult = Integer.compare(item1.type, item2.type);
    if (typeCompareResult != 0) {
    return typeCompareResult;
    } else {
    return Double.compare(item1.price, item2.price);
    }
    }
    });


    EDIT: This is old school way of doing things. For start this is good, but ultimately take advantage of Comparator.comparingInt added in Java 8 which is more concise. Refer KaNa0011's answer



    Check Object ordering for more clarity.






    share|improve this answer















    You can do this using Collections.sort method. Just need to pass a custom Comparator implementation as below.



        List<Item> list = Arrays.asList(shoppingItems);
    Collections.sort(list, new Comparator<Item>() {
    @Override
    public int compare(Item item1, Item item2) {
    int typeCompareResult = Integer.compare(item1.type, item2.type);
    if (typeCompareResult != 0) {
    return typeCompareResult;
    } else {
    return Double.compare(item1.price, item2.price);
    }
    }
    });


    EDIT: This is old school way of doing things. For start this is good, but ultimately take advantage of Comparator.comparingInt added in Java 8 which is more concise. Refer KaNa0011's answer



    Check Object ordering for more clarity.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 6:19

























    answered Jan 4 at 3:26









    shriyogshriyog

    587818




    587818













    • Do I need anything else before or after this code? What sort of imports should I add?

      – Math is Life
      Jan 4 at 3:27











    • How do I solve a NullPointerException error?

      – Math is Life
      Jan 4 at 3:31











    • My bad, it's working now

      – shriyog
      Jan 4 at 3:33











    • Is there a way to do this using a for loop for price?

      – Math is Life
      Jan 4 at 5:41



















    • Do I need anything else before or after this code? What sort of imports should I add?

      – Math is Life
      Jan 4 at 3:27











    • How do I solve a NullPointerException error?

      – Math is Life
      Jan 4 at 3:31











    • My bad, it's working now

      – shriyog
      Jan 4 at 3:33











    • Is there a way to do this using a for loop for price?

      – Math is Life
      Jan 4 at 5:41

















    Do I need anything else before or after this code? What sort of imports should I add?

    – Math is Life
    Jan 4 at 3:27





    Do I need anything else before or after this code? What sort of imports should I add?

    – Math is Life
    Jan 4 at 3:27













    How do I solve a NullPointerException error?

    – Math is Life
    Jan 4 at 3:31





    How do I solve a NullPointerException error?

    – Math is Life
    Jan 4 at 3:31













    My bad, it's working now

    – shriyog
    Jan 4 at 3:33





    My bad, it's working now

    – shriyog
    Jan 4 at 3:33













    Is there a way to do this using a for loop for price?

    – Math is Life
    Jan 4 at 5:41





    Is there a way to do this using a for loop for price?

    – Math is Life
    Jan 4 at 5:41













    1














    You can sort them by using a custom comparator object created by chaining Comparator.comparing(...) calls.



    Comparator<Item> itemComparator = Comparator
    .comparingInt(Item::getType)
    .thenComparingDouble(Item::getPrice);

    Arrays.sort(soppingItems, itemComparator);





    share|improve this answer
























    • What's this class Comparator package references would be helpful

      – shriyog
      Jan 4 at 3:36






    • 1





      Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

      – KaNa0011
      Jan 4 at 3:41











    • So, it's the same old java.util.Comparator with added methods in Java 8 release

      – shriyog
      Jan 4 at 3:41











    • Is there a way to do this using a for loop?

      – Math is Life
      Jan 4 at 5:20











    • There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

      – KaNa0011
      Jan 4 at 6:28
















    1














    You can sort them by using a custom comparator object created by chaining Comparator.comparing(...) calls.



    Comparator<Item> itemComparator = Comparator
    .comparingInt(Item::getType)
    .thenComparingDouble(Item::getPrice);

    Arrays.sort(soppingItems, itemComparator);





    share|improve this answer
























    • What's this class Comparator package references would be helpful

      – shriyog
      Jan 4 at 3:36






    • 1





      Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

      – KaNa0011
      Jan 4 at 3:41











    • So, it's the same old java.util.Comparator with added methods in Java 8 release

      – shriyog
      Jan 4 at 3:41











    • Is there a way to do this using a for loop?

      – Math is Life
      Jan 4 at 5:20











    • There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

      – KaNa0011
      Jan 4 at 6:28














    1












    1








    1







    You can sort them by using a custom comparator object created by chaining Comparator.comparing(...) calls.



    Comparator<Item> itemComparator = Comparator
    .comparingInt(Item::getType)
    .thenComparingDouble(Item::getPrice);

    Arrays.sort(soppingItems, itemComparator);





    share|improve this answer













    You can sort them by using a custom comparator object created by chaining Comparator.comparing(...) calls.



    Comparator<Item> itemComparator = Comparator
    .comparingInt(Item::getType)
    .thenComparingDouble(Item::getPrice);

    Arrays.sort(soppingItems, itemComparator);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 at 3:21









    KaNa0011KaNa0011

    615616




    615616













    • What's this class Comparator package references would be helpful

      – shriyog
      Jan 4 at 3:36






    • 1





      Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

      – KaNa0011
      Jan 4 at 3:41











    • So, it's the same old java.util.Comparator with added methods in Java 8 release

      – shriyog
      Jan 4 at 3:41











    • Is there a way to do this using a for loop?

      – Math is Life
      Jan 4 at 5:20











    • There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

      – KaNa0011
      Jan 4 at 6:28



















    • What's this class Comparator package references would be helpful

      – shriyog
      Jan 4 at 3:36






    • 1





      Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

      – KaNa0011
      Jan 4 at 3:41











    • So, it's the same old java.util.Comparator with added methods in Java 8 release

      – shriyog
      Jan 4 at 3:41











    • Is there a way to do this using a for loop?

      – Math is Life
      Jan 4 at 5:20











    • There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

      – KaNa0011
      Jan 4 at 6:28

















    What's this class Comparator package references would be helpful

    – shriyog
    Jan 4 at 3:36





    What's this class Comparator package references would be helpful

    – shriyog
    Jan 4 at 3:36




    1




    1





    Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

    – KaNa0011
    Jan 4 at 3:41





    Comparator was an interface that holds a function for comparing objects in java. Since Java 8, they added static and default methods for building comparator Fp-style

    – KaNa0011
    Jan 4 at 3:41













    So, it's the same old java.util.Comparator with added methods in Java 8 release

    – shriyog
    Jan 4 at 3:41





    So, it's the same old java.util.Comparator with added methods in Java 8 release

    – shriyog
    Jan 4 at 3:41













    Is there a way to do this using a for loop?

    – Math is Life
    Jan 4 at 5:20





    Is there a way to do this using a for loop?

    – Math is Life
    Jan 4 at 5:20













    There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

    – KaNa0011
    Jan 4 at 6:28





    There is. You need to select first which algorithm you need to implement. For example, selection sort is one of the easiest algorithm to implement in a for loop - just 2 for loop that is nested, and a conditional statement that decides if the array elements should be swapped.

    – KaNa0011
    Jan 4 at 6:28











    0














        List<Item> list = Arrays.asList(shoppingItems);
    Collections.sort(list,Comparator.comparingInt(Item::getType));
    System.out.println(list.toString());


    Assume you have the corresponding getType() and toString() defined for your Item class






    share|improve this answer




























      0














          List<Item> list = Arrays.asList(shoppingItems);
      Collections.sort(list,Comparator.comparingInt(Item::getType));
      System.out.println(list.toString());


      Assume you have the corresponding getType() and toString() defined for your Item class






      share|improve this answer


























        0












        0








        0







            List<Item> list = Arrays.asList(shoppingItems);
        Collections.sort(list,Comparator.comparingInt(Item::getType));
        System.out.println(list.toString());


        Assume you have the corresponding getType() and toString() defined for your Item class






        share|improve this answer













            List<Item> list = Arrays.asList(shoppingItems);
        Collections.sort(list,Comparator.comparingInt(Item::getType));
        System.out.println(list.toString());


        Assume you have the corresponding getType() and toString() defined for your Item class







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 3:21









        devildeltadevildelta

        693




        693























            -1














            The Collections.sort() should be able to help you.



            Please have a look at this article (Sorting an ArrayList according to user defined criteria.):
            https://www.geeksforgeeks.org/collections-sort-java-examples/






            share|improve this answer



















            • 1





              Please avoid link only answers.

              – talex
              Jan 4 at 2:59
















            -1














            The Collections.sort() should be able to help you.



            Please have a look at this article (Sorting an ArrayList according to user defined criteria.):
            https://www.geeksforgeeks.org/collections-sort-java-examples/






            share|improve this answer



















            • 1





              Please avoid link only answers.

              – talex
              Jan 4 at 2:59














            -1












            -1








            -1







            The Collections.sort() should be able to help you.



            Please have a look at this article (Sorting an ArrayList according to user defined criteria.):
            https://www.geeksforgeeks.org/collections-sort-java-examples/






            share|improve this answer













            The Collections.sort() should be able to help you.



            Please have a look at this article (Sorting an ArrayList according to user defined criteria.):
            https://www.geeksforgeeks.org/collections-sort-java-examples/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 4 at 2:58









            Cyrus LeungCyrus Leung

            995




            995








            • 1





              Please avoid link only answers.

              – talex
              Jan 4 at 2:59














            • 1





              Please avoid link only answers.

              – talex
              Jan 4 at 2:59








            1




            1





            Please avoid link only answers.

            – talex
            Jan 4 at 2:59





            Please avoid link only answers.

            – talex
            Jan 4 at 2:59



            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas