0

I am using nvd3 chart in my angular application. I am getting the labels in below format

["14:49", "14:58", "14:57", "14:56", "14:55", "14:54", "14:53", "14:52", "14:51"]

I have to sort them based on time.This is my code

var hourMin = new Date($scope.items[i].timestamp);
$scope.labels[i] = hourMin.getHours() + ":" + hourMin.getMinutes();

HTML :

<canvas id="line" class="chart chart-line" chart-data="data"
                 chart-labels="labels" chart-legend="true" chart-series="series"
                 chart-click="onClick" >
  </canvas>

I have tried orderBy but it is not working. Help me.

1
  • when the time is a single digit, do you like to get something like "3:7" for 3 hours and 7 minutes? You should to use a angular filter to convert. If your date is normalized (hh:mm), a single string sort will works. Commented Nov 24, 2015 at 12:47

1 Answer 1

1

use this.......

<script>
  angular.module('app', [])
  .controller('test', ['$scope', function($scope){
    var test =  ["14:49", "14:58", "14:57", "14:56", "14:55", "14:54", "14:53", "14:52", "14:51"];
    test.sort(function (a, b) {
      return new Date('1970/01/01 ' + a) - new Date('1970/01/01 ' + b);
    });
    console.log(test);
  }])
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.