<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329549#M82238</link>
    <description>&lt;P&gt;You have to use the &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" target="_self"&gt;array.sort&lt;/A&gt; method, using a &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters" target="_self"&gt;compare function&lt;/A&gt; to define the sort order. Take a look at &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sorting_array_of_objects" target="_self"&gt;this section&lt;/A&gt; of the documentation that explains different ways of sorting arrays of objects. In this example, a simpler sort function uses the &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare" target="_self"&gt;locateCompare&lt;/A&gt; method to return the sorted titles.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const books = [
  { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
  { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
  { title: '1984', author: 'George Orwell', publicationYear: 1949 }
];
books.sort(function(a, b) {
  return a.title.localeCompare(b.title);
});

books.forEach((book) =&amp;gt; console.log(book.title));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Sep 2023 12:44:05 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2023-09-18T12:44:05Z</dc:date>
    <item>
      <title>JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329511#M82236</link>
      <description>&lt;P&gt;I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an array of books, and each book object has properties like 'title', 'author', and 'publicationYear'. How can I sort this array of book objects alphabetically by the 'title' property?&lt;/P&gt;
&lt;P&gt;Here's a simplified version of the array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const books = [
    { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
    { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
    { title: '1984', author: 'George Orwell', publicationYear: 1949 }
];&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;This array should be sorted ascendingly using the 'title' parameter. I attempted to discover the answer by going to several sites, but I was unable to locate the solution. Could you supply a JavaScript code sample that explains how to do this sorting and explains any key ideas or functions utilized in the code? I appreciate your help.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 14:17:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329511#M82236</guid>
      <dc:creator>hectorsalamanca</dc:creator>
      <dc:date>2023-12-13T14:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329543#M82237</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/648697"&gt;@hectorsalamanca&lt;/a&gt;&amp;nbsp;, take a look at this website for the full explanation but below is a code snippet that should sort your array of objects.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sorting_array_of_objects" target="_blank" rel="noopener"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sorting_array_of_objects&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// sort by title
books.sort((a, b) =&amp;gt; {
  const titleA = a.title.toUpperCase(); // ignore upper and lowercase
  const titleB = b.title.toUpperCase(); // ignore upper and lowercase
  if (titleA &amp;lt; titleB) {
    return -1;
  }
  if (titleA &amp;gt; titleB) {
    return 1;
  }

  // names must be equal
  return 0;
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 12:34:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329543#M82237</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2023-09-18T12:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329549#M82238</link>
      <description>&lt;P&gt;You have to use the &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" target="_self"&gt;array.sort&lt;/A&gt; method, using a &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters" target="_self"&gt;compare function&lt;/A&gt; to define the sort order. Take a look at &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sorting_array_of_objects" target="_self"&gt;this section&lt;/A&gt; of the documentation that explains different ways of sorting arrays of objects. In this example, a simpler sort function uses the &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare" target="_self"&gt;locateCompare&lt;/A&gt; method to return the sorted titles.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const books = [
  { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
  { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
  { title: '1984', author: 'George Orwell', publicationYear: 1949 }
];
books.sort(function(a, b) {
  return a.title.localeCompare(b.title);
});

books.forEach((book) =&amp;gt; console.log(book.title));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 12:44:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/javascript-array-of-objects-sorting-how-to-sort-by/m-p/1329549#M82238</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-09-18T12:44:05Z</dc:date>
    </item>
  </channel>
</rss>

