Are the sorting algorithms for BackboneJS and AngularJS considered stable sorting methods?

I'm considering incorporating BackboneJS and AngularJS into my app. However, I'm curious about the stability of sorting algorithms in these frameworks. Specifically, I would like to know if they maintain the order of previously sorted columns within a table or collection.

Answer №1

Regarding Angular, I'm not certain, but when it comes to Backbone, the stability of sorting may vary depending on how it is implemented.

The sorting of all collections in Backbone is handled by

Backbone.Collection.prototype.sort
. Upon inspecting this method in the source code here:

if (_.isString(this.comparator) || this.comparator.length === 1) {
  this.models = this.sortBy(this.comparator, this);
} else {
  this.models.sort(_.bind(this.comparator, this));
}

If the defined comparator for your collection is a string (like a model attribute name) or a function that takes only one argument, then the sorting is done using _.sortBy, as explained in _.sortBy.

This function returns a sorted copy of the list, with stability ensured...

Further investigations into the underlying implementation reveal that stability is maintained by utilizing the element index as a secondary sorting factor.

If the collection's comparator cannot be utilized with _.sortBy, then resorting is carried out using Array.prototype.sort, which does not ensure stability.

Answer №2

When using the orderBy filter in Angular (specifically v1.3), it sorts stably based on the original list rather than the current sorted list while using ng-repeat. This can result in unexpected outcomes, though it is not an unstable sort.

Consider a basic controller in an Angular app:

angular.module('myApp', [])
  .controller('SortingCtrl', function ($scope) {
    $scope.items = [
      {
        name: 'A',
        value1: 5,
        value2: 10,
        value3: 0
      }, {
        name: 'B',
        value1: 2,
        value2: 15,
        value3: 0
      }, {
        name: 'C',
        value1: 6,
        value2: 12,
        value3: 0
      }
    ];

    $scope.sort = {
      predicate: 'name'
    };

  });

Accompanied by a simple view:

<body ng-controller="SortingCtrl">

  <select ng-model="sort.predicate">
    <option>name</option>
    <option>value1</option>
    <option>value2</option>
    <option>value3</option>
  </select>

  <table>
    <tr>
      <th>name</th>
      <th>value1</th>
      <th>value2</th>
      <th>value3</th>
    </tr>
    <tr ng-repeat="item in items | orderBy:sort.predicate">
      <td>{{ item.name }}</td>
      <td>{{ item.value1 }}</td>
      <td>{{ item.value2 }}</td>
      <td>{{ item.value3 }}</td>
    </tr>
  </table>

</body>

If you initially sort by value1 or value2, the results will be as expected. However, if you then decide to sort by value3, with all values being 0, you might assume the table order remains the same. Surprisingly, it resorts to its original order A, B, C.

This behavior doesn't indicate that the orderBy filter in Angular isn't stable, but it may produce surprising outcomes. You can see this demonstrated in the following Plunker link: http://plnkr.co/edit/rwrMQXSsB9wU3KBnWZBk?p=preview

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty removing new or existing lines on StackBlitz

I attempted to experiment with React on StackBlitz, but I encountered a problem where I couldn't delete any lines of code. It seems that while I can add new lines of code, deleting them is not an option. Even when logging in with GitHub, the issue per ...

Redux sub-application in React

Is it possible to incorporate a sub application within my backbone application using react redux? The purpose would be to have a small number of pages, mainly to test out the concept. ...

How can we insert data at the bottom of a table to begin with?

I am looking to add information retrieved from the iTunes API to the end of a table. The first album I receive will be placed at the very end, with each subsequent album adding on while pushing the previous one up in the hierarchy. Any ideas on how I can ...

Tips for loading a partial view page in a specific element using Angular JS after clicking a button

I'm facing an issue with my AngularJS partial view page not loading after a button click. I've set up the initial rendering page, but when we click the button, the partial view page doesn't render properly because the angular modules are not ...

Is there a way to insert rows and columns into the table headers using React Material UI?

I am new to working with material UI and I am having trouble figuring out how to add columns in the table header. The image below shows what I am trying to achieve - under "Economics", there should be 3 columns, each of which will then have two more column ...

"Error: Unable to locate module" encountered during the transition to Webpack 5

I am in the process of upgrading my React application to webpack version 5.72.0 (previously on 4.42.1). As part of this update, I have also upgraded webpack-cli to version 4.9.2, webpack-dev-server to version 4.8.1, and babel to version 7.17.9. However, I ...

Add value to a progress bar over time until it reaches the designated timeout

I'm struggling to implement a loading bar with a fixed timeout requirement. The goal is for the bar to be completely filled within 5 seconds. While I have successfully created the HTML and CSS components, I am facing difficulty in developing the JavaS ...

Discovering the upcoming work schedule and day based on a provided set of working hours

I have a schedule array that outlines the working hours of a company for each day of the week. I am looking to determine if the company is currently open or closed, and if open, display the closing time. If closed at the current time, I want to show the n ...

What is the best way to arrange a GeoJSON features array based on a specific property value?

I need help sorting a GeoJSON file based on a property and then slicing it to keep only the top 5 features. For instance, I want to take this GeoJSON and arrange it in descending order by the incidents property: ... [ -75.1972382872565 ...