Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure if you have a requirement to implement your own sort algorithm. If you do not, then you can either have Card implement the Comparable interface or, what seems like a better solution since you may not always want to sort by the same criteria, create a Comparator class for Cards. The latter approach is better if you need to sort by logicFace some times and logicSuit other times. Then, using either of these approaches, you can use the built in Java sorting algorithms. It's likely that the sorting operation will be faster if you do it this way than if you implement the various sorting methods yourself (but maybe not :).</p> <p>Try something like this SSCCE:</p> <pre><code>import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * http://stackoverflow.com/questions/19577847/java-sorting-method-critique */ public class Q19577847 { static class Card { private String suit; private String face; private int logicSuit; private int logicFace; Card(String suit, String face, int logicSuit, int logicFace) { this.suit = suit; this.face = face; this.logicSuit = logicSuit; this.logicFace = logicFace; } String getSuit() { return suit; } String getFace() { return face; } int getLogicSuit() { return logicSuit; } int getLogicFace() { return logicFace; } @Override public String toString() { return String.format("Card{suit='%s', face='%s', logicSuit=%d, logicFace=%d}", suit, face, logicSuit, logicFace); } } static class CardFaceComparator implements Comparator&lt;Card&gt; { @Override public int compare(Card card1, Card card2) { return card2.getLogicFace() - card1.getLogicFace(); } } private static void print(List&lt;Card&gt; cards) { for(Card card: cards) { System.out.println(" " + card); } } public static void sortByFace(List&lt;Card&gt; pile) { Collections.sort(pile, new CardFaceComparator()); } public static void main(String... args) { List&lt;Card&gt; pile = new ArrayList&lt;Card&gt;(); pile.add(new Card("Heart", "a", 1, 1)); pile.add(new Card("Heart", "2", 1, 2)); pile.add(new Card("Heart", "3", 1, 3)); pile.add(new Card("Heart", "4", 1, 4)); pile.add(new Card("Heart", "5", 1, 5)); pile.add(new Card("Heart", "6", 1, 6)); pile.add(new Card("Heart", "7", 1, 7)); pile.add(new Card("Heart", "8", 1, 8)); pile.add(new Card("Heart", "9", 1, 9)); pile.add(new Card("Heart", "10", 1, 10)); pile.add(new Card("Heart", "J", 1, 10)); pile.add(new Card("Heart", "Q", 1, 10)); pile.add(new Card("Heart", "K", 1, 10)); pile.add(new Card("Heart", "A", 1, 11)); pile.add(new Card("Spade", "a", 2, 1)); pile.add(new Card("Spade", "2", 2, 2)); pile.add(new Card("Spade", "3", 2, 3)); pile.add(new Card("Spade", "4", 2, 4)); pile.add(new Card("Spade", "5", 2, 5)); pile.add(new Card("Spade", "6", 2, 6)); pile.add(new Card("Spade", "7", 2, 7)); pile.add(new Card("Spade", "8", 2, 8)); pile.add(new Card("Spade", "9", 2, 9)); pile.add(new Card("Spade", "10", 2, 10)); pile.add(new Card("Spade", "J", 2, 10)); pile.add(new Card("Spade", "Q", 2, 10)); pile.add(new Card("Spade", "K", 2, 10)); pile.add(new Card("Spade", "A", 2, 11)); pile.add(new Card("Diamond", "a", 3, 1)); pile.add(new Card("Diamond", "2", 3, 2)); pile.add(new Card("Diamond", "3", 3, 3)); pile.add(new Card("Diamond", "4", 3, 4)); pile.add(new Card("Diamond", "5", 3, 5)); pile.add(new Card("Diamond", "6", 3, 6)); pile.add(new Card("Diamond", "7", 3, 7)); pile.add(new Card("Diamond", "8", 3, 8)); pile.add(new Card("Diamond", "9", 3, 9)); pile.add(new Card("Diamond", "10", 3, 10)); pile.add(new Card("Diamond", "J", 3, 10)); pile.add(new Card("Diamond", "Q", 3, 10)); pile.add(new Card("Diamond", "K", 3, 10)); pile.add(new Card("Diamond", "A", 3, 11)); pile.add(new Card("Club", "a", 4, 1)); pile.add(new Card("Club", "2", 4, 2)); pile.add(new Card("Club", "3", 4, 3)); pile.add(new Card("Club", "4", 4, 4)); pile.add(new Card("Club", "5", 4, 5)); pile.add(new Card("Club", "6", 4, 6)); pile.add(new Card("Club", "7", 4, 7)); pile.add(new Card("Club", "8", 4, 8)); pile.add(new Card("Club", "9", 4, 9)); pile.add(new Card("Club", "10", 4, 10)); pile.add(new Card("Club", "J", 4, 10)); pile.add(new Card("Club", "Q", 4, 10)); pile.add(new Card("Club", "K", 4, 10)); pile.add(new Card("Club", "A", 4, 11)); System.out.println("Before Sort:"); print(pile); sortByFace(pile); System.out.println("After Sort:"); print(pile); } } </code></pre> <p><strong>Edit</strong></p> <p>The Java API defines <code>Comparator&lt;T&gt;</code> which can be read as "a Comparator for type T", meaning Comparator compares objects of type T.</p> <p><code>Comparator&lt;? super T&gt;</code> used in the Collections.sort method means that the Comparator passed in as a parameter to the method can compare objects whose type "is a" T or a subtype of T. In other words, if the type of the objects in the list is a T or a subtype of T, then that comparator can be used by the sort method to determine the appropriate order. So in this example, since the List is what needs to be sorted, since Card does not itself implement Comparable, you can create a Comparator and pass both the List and Comparator into the Collections.sort method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload