Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:18:00+00:00 2026-06-01T17:18:00+00:00

I am trying to sort an array using the insertion sort algorithm. The array

  • 0

I am trying to sort an array using the insertion sort algorithm. The array is filled with WordNode elements that include a word field (inputted from a text file) and a frequency field (to measure the number of times the particular word appears in the text file). I have implemented the sort so that words are sorted by frequency (from lowest to highest), but I also want to sort alphabetically if frequencies are equal. How can I sort using two different criteria at the same time? Below is my sort code.

public static void sort(ArrayUnorderedList<WordNode> array) {
    //create stacks for insertion sort
    LinkedStack<WordNode> sorted = new LinkedStack<WordNode>();
    LinkedStack<WordNode> temp = new LinkedStack<WordNode>();

    //while the array has elements to be sorted
    while(!array.isEmpty()) {
        //remove current element from array
        WordNode currentNode = array.removeFirst();

        //while the sorted stack meets sorting criteria
        while((!sorted.isEmpty()) && (sorted.peek().getFrequency() < currentNode.getFrequency())) {
            //push elements to temp stack
            temp.push(sorted.pop());
        }

        //push current element to sorted stack
        sorted.push(currentNode);

        //while the temp stack has elements to be replaced
        while(!temp.isEmpty()) {
            //push elements to sorted stack
            sorted.push(temp.pop());
        }
    }

    //replace sorted elements in array
    while(!sorted.isEmpty()) {
        array.addToRear(sorted.pop());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:18:01+00:00Added an answer on June 1, 2026 at 5:18 pm

    AppClay’s answer is absolutely correct, but if you are interested in “tidying it up”, create a helper that implements Comparator.

    class WordNodeComparator implements Comparator<WordNode> {
        @Override
        public int compare(WordNode lhs, WordNode rhs) {
            int result = lhs.getFrequency() - rhs.getFrequency();
            if (result == 0) {
                return lhs.getWord().compareTo(rhs.getWord());
            }
            else {
                return result;
            }
        }
    }
    

    Then you simply create an instance of it, and use it in your loop:

    while((!sorted.isEmpty()) && (nodeComparator.compare(sorted.peek(), currentNode) < 0)
    

    Not only does this make the code easier to read and test, it’s now trivial to swap out different Comparator implementations as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort an array, filled with objects from a class i wrote.
I'm trying to sort a multidimensional array in objective-c i know that i can
I'm trying to pre-sort an array of items are added into an array using
I am trying to sort my locations array from shortest to longest distance with
I am trying to use qsort from STL to sort array of edge: struct
I am trying to sort an array of integers in MIPS using bubble sort
I'm trying to sort an array that looks like this: var dateGroups = [
Trying to sort an array by writing my own sort method using recursion (Pine's
I am trying to sort an array using multithreading in java and note its
I am trying to sort a multidimensional JSON array (called jsontest here) using JQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.