0

$(function() {
  var companyId = 1740963;
  JA.get('api/' + companyId + '/Transaction', function(data) {
    if (!data)
      return;
    var items = data.Items;
    if (!items)
      return;
    for (var i = 0; i < items.length; i++) {
      var expense = items[i];
      JA.renderTemplateFromPath('expenseListRow', items[i], function(template) {
        var $template = $(template);
        $('#expenseListBody').append($template);
        $template.find('.expenseDate p').html(function(index, value) {
          return moment(value, "YYYY-MM-DDTHH:mm:ss").format("DD/MM/YYYY");
        });
      })
    }
  });
})
<table id="expenseList">

  <tr id="expenseListHead">

    <th id="numberID">Number</th>

    <th>Date</th>

    <th>Name</th>

    <th>Description</th>

    <th>Reference</th>

    <th class="alignRight">Amount</th>

    <th class="alignRight"></th>

  </tr>

  <tbody id="expenseListBody">

    <tr class="expenseListRow">

      <td></td>

      <td class="expenseDate"></td>

      <td></td>

      <td></td>

      <td></td>

      <td class="alignRight"></td>

      <td class="alignRight"></td>

    </tr>

  </tbody>

</table>

enter image description here

As you can see in the image, there are multiple numbers in random order being pulled from an API, the list carry's on for a while. What I am wondering is if anyone could help me work out how to sort these numbers in ascending order (starting from 1) and so on, instead of it adding them randomly, like I said this data is being pulled from an API that has been written so I'm struggling to work out how to do this. If anymore info is needed to help answer let me know. Thanks in advance.

  • please add all relevant parts in text form to the question, you might have a look here, too: [mcve] – Nina Scholz May 30 '17 at 09:35
  • @JacobHarrison .Please add you code snippet.so that we can rectify there. Through image it's impossible to solve your problem – Alive to die - Anant May 30 '17 at 09:37
  • @AlivetoDie I have added it, however as you can see when you run it no data is getting pulled through on here, hence the screenshot, just want to be pointed in the direction of how I could sort the numbers – Jacob Harrison May 30 '17 at 09:40
  • You can refer this link you may get some idea, After getting json data, you can sort and then bind. https://stackoverflow.com/questions/8175093/simple-function-to-sort-an-array-of-objects – Vaibhav shetty May 30 '17 at 09:45

1 Answers1

0

If your list contains only numbers you can use the sort function available with Array. just call

var sortedList = yourList.sort();

If its an array of objects and the numbers are inside as a field then you can override the sort function as mentioned here

BiJ
  • 1,639
  • 5
  • 24
  • 55