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 978953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:08:30+00:00 2026-05-16T04:08:30+00:00

sort() orders the elemts alphabetically: var fruits = new Array("Apple", "Banana", "Kiwi", "Ananas", "Mango");

  • 0

sort() orders the elemts alphabetically:

var fruits = new Array("Apple", "Banana", "Kiwi", "Ananas", "Mango");
Namen.sort();
document.write(fruits);

Well, I don’t want the alphabetical order…it should be like

var reorderFruits = new Array("Kiwi", "Apple", "Mango", "Banana", "Ananas");

Is it possible to reorder the order the way I imagined it? How?

Thank you in advance.
Faili

Edit:

<input type="checkbox" id="apple" checked="checked" />
<label for="apple">Apple</label>
<input type="checkbox" id="Banana" checked="checked" />
<label for="Banana">Banana</label>
<input type="checkbox" id="Kiwi" />
<label for="Kiwi">Kiwi</label>
<input type="checkbox" id="Mango" checked="checked"/>
<label for="Mango">Mango</label>

Well… I get the information from a website I have no access to. No, I want to reorder the order.
In this example it is:

Apple -> Banana -> Kiwi -> Mango

Now I’m asking whether it is possible to make it to:

Kiwi -> Banana -> mango -> Apple

Please ask whether there is sth unclear.

Thank you so much.

  • 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-05-16T04:08:31+00:00Added an answer on May 16, 2026 at 4:08 am

    This is just an example that will sort the array by the length of the item. It will match your desired output on any browser that uses a stable sorting algorithm (which is most of them now). You can modify the function to sort by any other criteria you want as well.

    var fruits = new Array("Apple", "Banana","Kiwi",  "Ananas", "Mango");
    fruits.sort(function(a,b) {
        return a.length - b.length;
    });
    document.write(fruits);
    

    There are some rules with this. The sort function must compare a and b such that:

    • It returns a numeric value indicated whether a is less than, equal to, or greater than b
    • Multiple comparisons between the same a and b can happen, and should always return the same result for the same a and b.
    • Equal values must always return 0.

    I mention all this because one common (wrong) use for this is to randomize or shuffle an array such the sort function returns a random value. That will appear to work, but isn’t really random and can cause bugs later.

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

Sidebar

Related Questions

I would like to sort an array in ascending order using C/C++ . The
We have a series of drop down controls that determine the sort order of
System.IO.Directory.GetFiles() returns a string[] . What is the default sort order for the returned
Anyone have a good resource or provide a sample of a natural order sort
I have a small program to order and sort email messages, outputting to a
I'm trying to order items based on an attribute value: <xsl:apply-templates select=Question> <xsl:sort order=ascending
Similar to List<> OrderBy Alphabetical Order , we want to sort by one element,
I often have to sort a dictionary (consisting of keys & values) by value.
I have a list of data that users are able to control the sort
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end

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.