Sort Array of Objects By Two Properties in JavaScript

Sort Array of Objects By Two Properties in JavaScript

One sort function, two sort orders

I was recently working on a way to sort an array of objects alphabetically based on two columns. This means that both levels of alphabetical sorting should be maintained. See the example below.

var fruits = [
  {colA: 'apple', colB: 'red delicious'},
  {colA: 'pear', colB: 'red anjou'},
  {colA: 'orange', colB: 'navel'},
  {colA: 'orange', colB: 'blood'},
  {colA: 'pear', colB: 'green anjou'},
  {colA: 'apple', colB: 'granny smith'},
  {colA: 'orange', colB: 'clementine'}

The above list is supposed to be sorted to look like the below array.

  {colA: "apple", colB: "granny smith"},
  {colA: "apple", colB: "red delicious"},
  {colA: "orange", colB: "blood"},
  {colA: "orange", colB: "clementine"},
  {colA: "orange", colB: "navel"},
  {colA": "pear", colB: "green anjou"},
  {colA: "pear", colB: "red anjou"}

Now, your first though might be to simply sort the fruits by colA first. Then once you have a new sorted array, perform the second sort based on colB. Let’s see what happens if we do that.

code javascript sorting-algorithms programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Quick Sort: The most performant sorting algorithm

Get to know the most efficient sorting algorithm. Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array an element called a pivot is selected. All elements smaller ...

Sorting Algorithms With Javascript

Sorting Algorithms With Javascript. We will learn some of the basic and popular sorting algorithms. But before we directly jump on the topic let's first understand some parameters used to describe/evaluate a sorting algorithm. The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Sorting Algorithms: the Basics with JavaScript implementation

Nail the technical interview question. Sorting algorithms are part of the basics of computer science. They might not be something you need on a daily basis as a developer, but it is a very common interview question. There are tens of algorithms, some of them only useful in very special cases. In this article, we are going to go over the four most common ones, the ones you have to have heard of and might be asked about.