Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Sorting Method Critique
    primarykey
    data
    text
    <p>Could I please get your feedback on this sorting method I wrote? I'm specifically looking for ideas on anything that may be redundant/unnecessary or rewritten in a more efficient and/or readable manner, or just any feedback really.</p> <p>Essentially the method receives an ArrayList of Objects and sorts them in descending order based on an instance variable of type int that each Object contains. Specifically in my program it sorts by "int logicFace". </p> <p>I tried to create it by just swapping cards in the same passed-in ArrayList, but the shifting of indices caused by removing a card made the code more complicated, and I felt like removing from one array list and inserting into another separate ArrayList was more clean for a human to read. </p> <p>Here is the relevant Card instance variables, with standard getters:</p> <pre><code>public class Card { private String suit; private String face; private int logicSuit; private int logicFace; </code></pre> <p>And here is the method I would like your thoughts on:</p> <pre><code>public ArrayList&lt;Card&gt; sortByFace(ArrayList&lt;Card&gt; pile){ ArrayList &lt;Card&gt; sortedPile = new ArrayList &lt;Card&gt;(); int originalPileSize = pile.size(); int cardsRemoved = 0; while(cardsRemoved &lt; originalPileSize){ int highCardIndex = 0; while(true){ boolean swapPerformed = false; for(int i = 0; i &lt; pile.size(); i++){ if(pile.get(i).getLogicFace() &gt; pile.get(highCardIndex).getLogicFace()){ highCardIndex = i; if(!swapPerformed){ swapPerformed = true; } } } if(!swapPerformed){ sortedPile.add(pile.remove(highCardIndex)); cardsRemoved++; break; } } } pile = sortedPile; return pile; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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