www.webdeveloper.com
Results 1 to 9 of 9

Thread: sorting decimal numbers correctly

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    Exclamation sorting decimal numbers correctly

    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.

    Code:
    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,
    jmcall10

  2. #2
    Join Date
    Dec 2012
    Posts
    95
    You're only sorting by the name, you need to sort by the value as well.

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

  3. #3
    Join Date
    Oct 2004
    Posts
    11
    Ok,

    I have implemented your solution and it has now lost the initial Alphabetic sorting. I cannot deduce what method it is using to sort now :S

  4. #4
    Join Date
    Dec 2012
    Posts
    95
    What format are the names/values your sorting in? I'm assuming it's an array of objects like this

    Code:
    var array = [
       {
          "myname":"APPLE",
          "value":1.0051
       },
       {
          "myname":"APPLE",
          "value":1.1071
       }
    ];
    And then you're using the sort function, passing through the function above as the parameter like so

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

  5. #5
    Join Date
    Oct 2004
    Posts
    11
    Yes effectively that is what is happening.

    However the value is within the myname variable. for example "Apple - 1.0051" is the full myname.

  6. #6
    Join Date
    Dec 2012
    Posts
    95
    This should do it

    Code:
    function(a, b){
    	var splitA = a.myname.split("-");
    	var splitB = b.myname.split("-");
    	var nameA=splitA[0].toLowerCase(), nameB=splitB[0].toLowerCase()
    	var valA=parseFloat(splitA[1]), valB=parseFloat(splitB[1]);
    	if (nameA < nameB || valA < valB) //sort string ascending
    	return -1 
    	if (nameA > nameB || valA > valB)
    	return 1
    	return 0 //default return value (no sorting)
    }

  7. #7
    Join Date
    Oct 2004
    Posts
    11
    Ok, this code is now not sorting alphabetically at all. Is there perhaps and error in the code?

    It should sort alphabetically and then numerically.

    Thanks again for looking into this for me.

  8. #8
    Join Date
    Oct 2004
    Posts
    11
    Ok got it working from another forum using this code:
    Code:
    function(a, b) {
        var parts = {
            a: a.myname.split(' - '),
            b: b.myname.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
    }

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259

    Lightbulb

    Glad you figured it out. Took me too long to replicate you data. Here is an alternative method...
    Code:
    <div id="debugger"></div>
    
    <script type="text/javascript">
    // Modified from: http://answers.yahoo.com/question/index?qid=20070718104153AA232co
    
    function addMarkerObj(arr, nam, num){ arr[arr.length] = {distance:num, name:nam}; }
    
    function sortNumberObj(a, b){ return a.distance - b.distance; }                    //no need for an array index with objects
    function sortAlphaObj(a, b){ return a.name.toLowerCase() > b.name.toLowerCase(); } //no need for an array index with objects
    
    function displayMarkersObj(arr){
      var str = "";
      for(var i = 0; i < arr.length; i++) {
        str += "<br>distance: " + arr[i].distance + " name: " + arr[i].name;  // change display sequence as desired
      }
      return str;
    }
    
    var rmarkers = [];
    addMarkerObj(rmarkers, 'APPLE', 1.0051);
    addMarkerObj(rmarkers, 'APPLE', 1.1071);
    addMarkerObj(rmarkers, 'APPLE', 11.1592);
    addMarkerObj(rmarkers, 'APPLE', 12.0692);
    addMarkerObj(rmarkers, 'APPLE', 12.1717);
    addMarkerObj(rmarkers, 'APPLE', 2.0186);
    addMarkerObj(rmarkers, 'APPLE', 21.1407);
    addMarkerObj(rmarkers, 'APPLE', 22.089);
    addMarkerObj(rmarkers, 'APPLE', 23.069);
    addMarkerObj(rmarkers, 'BANANA', 1.0051);
    addMarkerObj(rmarkers, 'BANANA', 1.1071);
    addMarkerObj(rmarkers, 'BANANA', 11.1592);
    addMarkerObj(rmarkers, 'BANANA', 12.0692);
    addMarkerObj(rmarkers, 'BANANA', 12.1717);
    addMarkerObj(rmarkers, 'BANANA', 2.0186);
    addMarkerObj(rmarkers, 'BANANA', 21.1407);
    addMarkerObj(rmarkers, 'BANANA', 22.089);
    addMarkerObj(rmarkers, 'BANANA', 23.069);
    
    var str = '';
    str = displayMarkersObj(rmarkers);
    document.getElementById('debugger').innerHTML = 'Un-sorted:'+str;
    
    rmarkers.sort(sortNumberObj);
    str = displayMarkersObj(rmarkers);
    document.getElementById('debugger').innerHTML += '<p>Distance sort:'+str;
    
    rmarkers.sort(sortAlphaObj);
    str = displayMarkersObj(rmarkers);
    document.getElementById('debugger').innerHTML += '<p>Name sort:'+str;
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles