'Pouch DB data is not inserted in sorted order

I am working on small form in electron and pouchdb.

When inserting a data into db, until the ninth doc it inserting perfectly, but on 10th doc it is inserting to second position of doc instead on inserting into last

before inserting the 10th doc

{
  "total_rows": 9,
  "offset": 0,
    "rows": [
    { "id": "1", "key": "1", "value": {...} },
    { "id": "2", "key": "2", "value": {...} },   
    { "id": "3", "key": "3", "value": {...} },
    { "id": "4", "key": "4", "value": {...} }    
    { "id": "5", "key": "5", "value": {...} },
    { "id": "6", "key": "6", "value": {...} },   
    { "id": "7", "key": "7", "value": {...} },
    { "id": "8", "key": "8", "value": {...} },
    { "id": "9", "key": "9", "value": {...} }
  ]
}

After inserting 10th doc

{
  "total_rows": 10,
  "offset": 0,
    "rows": [
    { "id": "1", "key": "1", "value": {...} },
    { "id": "10", "key": "10", "value": {...} },
    { "id": "2", "key": "2", "value": {...} },   
    { "id": "3", "key": "3", "value": {...} },
    { "id": "4", "key": "4", "value": {...} }    
    { "id": "5", "key": "5", "value": {...} },
    { "id": "6", "key": "6", "value": {...} },   
    { "id": "7", "key": "7", "value": {...} },
    { "id": "8", "key": "8", "value": {...} },
    { "id": "9", "key": "9", "value": {...} }
  ]
}

here attached the js code

// form submit
const form = document.getElementById("form1");
form.addEventListener("submit", dbsubmit);

function dbsubmit(e) {
   e.preventDefault();
   //   getting values from form
   var Sno = document.getElementById("number").value
   var date = document.getElementById("date").value;
   var Time = document.getElementById("time").value;
   var Trip = document.querySelector('input[name="Trip"]:checked').value;
   var TripType = document.querySelector('input[name="Type"]:checked').value;
 
 // assigning form values to db table names
  var doc = {
    _id: Sno,
    Date: date,
    time: Time,
    trip: Trip,
    triptype: TripType,
  };

   // inserting to db
  db.put(doc, function(err, response) {
    if (err) {
      return console.log(err);
    } else {
      console.log("Document created Successfully", response);
    }
  });
}

Inserting until ninth 9th doc it's perfectly inserting but when inserting the 10th doc it is inserting in second position.

The doc should be sorted by id and want to use it to view in another page.

I debugged but cannot find a solution, can anyone help me with the solution?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source