43539

Javascript not sorting DECIMAL numbers correctly

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) }

Answer1:

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 }

Answer2:

I think you almost got it. You just forgot to compare the numbers:

function(x, y){ var xName=x.myname.toLowerCase(), yName=y.myname.toLowerCase(); if (xName < yName){ //string sort ascending return -1 }else if (xName < yName){ return 1 }else if (x.value < y.value){ return -1; }else if (x.value > y.value){ return 1; }else{ return 0; // Identical keys } }

Recommend

  • iCloud: Sync Core Data between IOS and OSX
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • php is_file always return false
  • Display images in Django
  • How to know when stdin is empty if it contains EOF?
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Replace value with Factor in r data.table
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Jquery UI tool tip close icon
  • $wpdb not working in file of WordPress plugin
  • jQuery .attr() and value
  • Jetty Server not starting: Unable to establish loopback connection
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • DomPDF {PAGE_NUM} not on first page
  • R - Combining Columns to String Based on Logical Match
  • how to do an event when i swipe from fragment to the other
  • Linq Objects Group By & Sum
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • How reduce the height of an mschart by breaking up the y-axis
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • Knitr HTML Loop - Some HTML output, some R output
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal