Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Sorting by Alphabetical Order

I am trying to output the JSON below to HTML. I have created the object along with the properties for this object however, I am struggling to output this to HTML. Here is what I have so far. Any idea?

var people;

people = [{
    first_name: 'Don Molloy',
    category: 'academic'
  },
  {
    first_name: 'Pat Quill',
    category: 'academic'
  }
];

console.log(people);

var myObj = JSON.parse(people);
document.getElementById("output-people").innerHTML = myObj.name;
<p id="output-people"></p>

thanks,

like image 415
Kerbol Avatar asked May 16 '17 09:05

Kerbol


People also ask

How to sort JSON string / file alphabetically?

An online tool to sort JSON string / file alphabetically. It's very easy. Just need to provide the JSON string content.Sort the JSON content alphabetically without disturbing the JSON structure. It just change the postion of the element at the same level. It sort the JSON String considering JSON object and its value.

How to sort JSON objects by key name or key value?

Use JSON Sort tool for sorting JSON objects by key name or key value. The object can be ordered alphabetically, numerically, either ascending or descending (reversed). The JSON sorter tool can be used as a formatting utility, pretty printer to create a legible and indented format. Additionally, it will validate and indicate any parsing errors.

Should JSON objects be alphabetized by key name?

If you’ve generated JSON objects via FileMaker, you’re likely aware of the disparity between the order in which you specify the elements, and the order in which they subsequently appear (i.e., alphabetized by key name). As I wrote two years ago (in Thinking About JSON, Part 2 )…

How to sort array alphabetically in JavaScript?

How to Sort Array Alphabetically in JavaScript. JavaScript arrays have the sort ( ) method, which sorts the array elements into alphabetical order. The sort ( ) method accepts a function that compares two items of the Array.sort ( [comparer]) . let elements = [ 'Javascript', 'Css', 'Html' ]; elements.sort ( function (a, b) { return a.length - b.


Video Answer


2 Answers

This not json Object .its a normal array Object.so remove the JSON.parse() .And change the output innerHTML dom like this myObj[0].first_name

Update:

For all first_name with sort .try this myObj.map(a=> a.first_name).sort().join(" and ")

var people;

people = [{
    first_name: 'Don Molloy',
    category: 'academic'
  },
  {
    first_name: 'Pat Quill',
    category: 'academic'
  }
];

people = people.sort((a,b)=> {
var a1 = a.first_name.toLowerCase();
var b1 = b.first_name.toLowerCase();
return a1<b1 ?-1:a1> b1? 1 :0;
})
console.log(people);

var myObj =people;
document.getElementById("output-people").innerHTML = myObj.map(a=> a.first_name).join(" and ")
<p id="output-people"></p>
like image 147
prasanth Avatar answered Oct 02 '22 23:10

prasanth


As of your question "JSON Sorting by Alphabetical Order" you can first use Array.prototype.sort() to sort the array and than populate the output variable and also use the separator the way you want to show the names in the html.

Code:

const people = [{first_name: 'Don Molloy',category: 'academic'}, {first_name: 'Pat Quill',category: 'academic'}];
const separator = '<br>';

// Sort array of objects by first_name and populate the output with separator
const output = people
  .sort((a, b) => a.first_name.toLowerCase().localeCompare(b.first_name.toLowerCase()))
  .map(user => user.first_name)
  .join(separator);

console.log(people);
document.getElementById('output-people').innerHTML = output;
<p id="output-people"></p>
like image 33
Yosvel Quintero Arguelles Avatar answered Oct 02 '22 23:10

Yosvel Quintero Arguelles