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

  • Home
  • SEARCH
  • 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 9247045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:38:08+00:00 2026-06-18T09:38:08+00:00

Ok I have a section of code that sorts the names it is given

  • 0

Ok I have a section of code that sorts the names it is given alphabetically.

However the code doesnt handle decimals the way I would want.

It orders the name in the following manner (Obv I would rather it incremented numerically):

It would order it:

  • APPLE – 1.0051
  • APPLE – 1.1071
  • APPLE – 11.1592
  • APPLE – 12.0692
  • APPLE – 12.1717
  • APPLE – 2.0186 << this should be after “APPLE – 1.1071” obviously
  • APPLE – 21.1407
  • APPLE – 22.089
  • APPLE – 23.069
  • BANANA – 1.0051
  • BANANA – 1.1071
  • BANANA – 11.1592
  • BANANA – 12.0692
  • BANANA – 12.1717
  • BANANA – 2.0186 << this should be after “BANANA – 1.1071” obviously
  • BANANA – 21.1407
  • BANANA – 22.089
  • BANANA – 23.069

Here is the code I am using. I do not fully understand the code as it was a snippet I have been using.

function(a, b){
         var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()
         if (nameA < nameB) //sort string ascending
          return -1 
         if (nameA > nameB)
          return 1
         return 0 //default return value (no sorting)
    }

Regards,
Jonny

  • 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-18T09:38:10+00:00Added an answer on June 18, 2026 at 9:38 am

    Demo and here is the source + small explanation:

    function fruit_sort(a, b) {
        var parts = {
            a: a.split('-'),
            b: b.split('-')
        };
        if (parts.a[0] == parts.b[0]) // strings are the same
            return parseFloat(parts.a[1]) - parseFloat(parts.b[1]); // sort by number
        return parts.a[0] > parts.b[0] ? 1 : -1; // sort by string
    }
    var arr = ["APPLE - 1.0051",
        "APPLE - 1.1071",
        "APPLE - 11.1592",
        "APPLE - 12.0692",
        "BANANA - 1.0051",
        "BANANA - 1.1071",
        "BANANA - 11.1592",
        "BANANA - 12.0692",
        "BANANA - 12.1717",
        "APPLE - 12.1717",
        "APPLE - 2.0186",
        "APPLE - 21.1407",
        "BANANA - 23.069",
        "APPLE - 22.089",
        "APPLE - 23.069",
        "BANANA - 2.0186",
        "BANANA - 21.1407",
        "BANANA - 22.089"];
    arr.sort(fruit_sort);
    console.log(arr);
    // outputs
    [
        "APPLE - 1.0051",
        "APPLE - 1.1071",
        "APPLE - 2.0186",
        "APPLE - 11.1592",
        "APPLE - 12.0692",
        "APPLE - 12.1717",
        "APPLE - 21.1407",
        "APPLE - 22.089",
        "APPLE - 23.069",
        "BANANA - 1.0051",
        "BANANA - 1.1071",
        "BANANA - 2.0186",
        "BANANA - 11.1592",
        "BANANA - 12.0692",
        "BANANA - 12.1717",
        "BANANA - 21.1407",
        "BANANA - 22.089",
        "BANANA - 23.069"
    ]
    

    First the function splits the terms up into their text and numerical parts – if the text is even it only sorts on the parseFloat value of the numerical value – otherwise it sorts first by the string value.

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

Sidebar

Related Questions

I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
I have this section of code that saves a list item on click to
Javascript I have code that will hide various sections in a MS CRM form
I have a section of code which should be executed by a maximum number
I have this view (DetailsContainer, first class in code section of this question) with
I have some VERY simple code to return the title for a section header:
In my xaml code, inside the Window.Resources section, I have defined a Data Template
I have a code that has a large number of malloc s and device-specific
I have the following code that should select all the users in the relevant

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.