1"use strict";(globalThis.webpackChunkdocusaurus_blog=globalThis.webpackChunkdocusaurus_blog||[]).push([[4023],{8453:(n,e,r)=>{r.d(e,{R:()=>i,x:()=>a});var s=r(6540);const t={},o=s.createContext(t);function i(n){const e=s.useContext(o);return s.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function a(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(t):n.components||t:i(n.components),s.createElement(o.Provider,{value:e},n.children)}},8458:(n,e,r)=>{r.r(e),r.d(e,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>i,metadata:()=>s,toc:()=>l});const s=JSON.parse('{"id":"Coding/LodashFunctions/Easy/sort-array","title":"\ud83d\udcc4 Sort Array","description":"Question Description","source":"@site/docs/Coding/LodashFunctions/Easy/sort-array.md","sourceDirName":"Coding/LodashFunctions/Easy","slug":"/sort-array","permalink":"/docs/sort-array","draft":false,"unlisted":false,"editUrl":"https://github.com/wuzhe0912/blog-docusaurus/tree/master/docs/Coding/LodashFunctions/Easy/sort-array.md","tags":[],"version":"current","frontMatter":{"id":"sort-array","title":"\ud83d\udcc4 Sort Array","slug":"/sort-array"},"sidebar":"coding","previous":{"title":"\ud83d\udcc4 String Operation","permalink":"/docs/string-operation"},"next":{"title":"\ud83d\udcc4 Find Most Frequent Char","permalink":"/docs/find-most-frequent-char"}}');var t=r(4848),o=r(8453);const i={id:"sort-array",title:"\ud83d\udcc4 Sort Array",slug:"/sort-array"},a=void 0,c={},l=[{value:"Question Description",id:"question-description",level:2},{value:"Ascending Sort",id:"ascending-sort",level:3},{value:"Descending Sort",id:"descending-sort",level:3},{value:"Intentionally Inserting Strings",id:"intentionally-inserting-strings",level:3},{value:"Object Sorting",id:"object-sorting",level:3}];function d(n){const e={code:"code",h2:"h2",h3:"h3",li:"li",ol:"ol",p:"p",pre:"pre",...(0,o.R)(),...n.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(e.h2,{id:"question-description",children:"Question Description"}),"\n",(0,t.jsxs)(e.p,{children:["Given an array of numbers, use the ",(0,t.jsx)(e.code,{children:"sort"})," function to sort the array. Provide solutions for both scenarios:"]}),"\n",(0,t.jsxs)(e.ol,{children:["\n",(0,t.jsx)(e.li,{children:"Ascending order (smallest to largest)"}),"\n",(0,t.jsx)(e.li,{children:"Descending order (largest to smallest)"}),"\n"]}),"\n",(0,t.jsx)(e.h3,{id:"ascending-sort",children:"Ascending Sort"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-js",children:"const numbers = [10, 5, 50, 2, 200];\n\n// Using a comparison function\nnumbers.sort(function (a, b) {\n return a - b;\n});\n\nconsole.log(numbers); // [2, 5, 10, 50, 200]\n"})}),"\n",(0,t.jsx)(e.h3,{id:"descending-sort",children:"Descending Sort"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-js",children:"const numbers = [10, 5, 50, 2, 200];\n\nnumbers.sort(function (a, b) {\n return b - a;\n});\n\nconsole.log(numbers); // [200, 50, 10, 5, 2]\n"})}),"\n",(0,t.jsx)(e.h3,{id:"intentionally-inserting-strings",children:"Intentionally Inserting Strings"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-js",children:"const mixedNumbers = [10, '5', 50, '2', 200];\n\nmixedNumbers.sort(function (a, b) {\n return Number(a) - Number(b);\n});\n\nconsole.log(mixedNumbers); // ['2', '5', 10, 50, 200]\n"})}),"\n",(0,t.jsxs)(e.p,{children:["However, this solution cannot handle strings that cannot be converted to numbers, such as ",(0,t.jsx)(e.code,{children:"'iphone'"}),", ",(0,t.jsx)(e.code,{children:"'ipad'"}),", etc. These strings are converted to ",(0,t.jsx)(e.code,{children:"NaN"}),", and while they may end up at the end of the sorted array, different browsers may produce different results. In such cases, consider using ",(0,t.jsx)(e.code,{children:"filter"})," first to remove and restructure the array."]}),"\n",(0,t.jsx)(e.h3,{id:"object-sorting",children:"Object Sorting"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-js",children:"const mockArray = [\n { type: 'a', label: 1 },\n { type: 'a', label: 2 },\n { type: 'c', label: 1 },\n { type: 'c', label: 3 },\n { type: 'b', label: 2 },\n];\n"})})]})}function u(n={}){const{wrapper:e}={...(0,o.R)(),...n.components};return e?(0,t.jsx)(e,{...n,children:(0,t.jsx)(d,{...n})}):d(n)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.