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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:37:03+00:00 2026-06-18T06:37:03+00:00

I have some code that sorts names it is given alphabetically. The problem I

  • 0

I have some code that sorts names it is given alphabetically.
The problem I am having is with the way it handles decimals.
It orders the names like below (i would rather it increment numerically):

DOG - 1.0510
DOG - 1.1031
DOG - 11.1792
DOG - 12.0920
DOG - 12.1170
DOG - 2.0186 <-- should be after "DOG - 12.117" ???
DOG - 21.4070
DOG - 22.0790
DOG - 23.0390
CAT - 1.0810
CAT - 1.1071
CAT - 11.1592
CAT - 12.0691
CAT - 12.1718
CAT - 2.0186 <-- again should be after "CAT -12.1718" ???
CAT - 21.1403
CAT - 22.081
CAT - 23.069

I have the name/values inside an array of objects like below:

var array = [
   {
      "myname":"DOG",
      "value":1.0051
   },
   {
      "myname":"DOG",
      "value":1.1071
   }
];

Here is the code I am using from a snippet I found online.

function(x, y){
             var xName=x.myname.toLowerCase(), yName=y.myname.toLowerCase()
             if (xName < yName) //string sort ascending
              return -1 
             if (xName < yName)
              return 1
             return 0 //return default value (without sorting)
        }
  • 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-18T06:37:04+00:00Added an answer on June 18, 2026 at 6:37 am

    Think the problem is that you need to sort by the value as well but you are only sorting by the name.
    The below should fix it, it sorts alphabetically and then numerically.

    Code:

    function(x, y) {
        var parts = {
            x: x.myname.split(' - '),
            y: y.myname.split(' - ')
        };
        if (parts.x[0] == parts.y[0]) // strings are the same
            return parseFloat(parts.x[1]) - parseFloat(parts.y[1]); // sort by number
        return parts.x[0] > parts.y[0] ? 1 : -1; // sort by string
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this: foreach(var obj in collection) { try
I have a program that is trying to sort some names alphabetically. I run
I have some old code that uses qsort to sort an MFC CArray of
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after
I have some code that runs a model in a loop. Each iteration of
I have some code that shows results in a table Right now it will
I have some code that uses the SQL Server 2005 SMO objects to backup

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.