🌱 Digital garden
  • Welcome
  • Courses
    • Fundamentals of 2D Game Engines with C++ SDL and Lua
      • Dependencies and project structure
  • Computer science
    • Basic
  • Sound
    • FM synthesis
  • Development
    • Design system
    • Text editors
      • Vim
      • Emacs
      • VS Code
    • Databases
      • MongoDB
    • NES Development
    • Programming languages
      • Shell
      • Javascript
        • Arrays
        • JSDoc
    • UML
    • Web development
      • HTTP
      • GraphQL
      • PWA
      • CSS
      • REST
      • Web scrapping
      • React
        • React hooks
      • HTML5
        • Canvas
          • WebGL
          • Context2D
    • Game development
      • Pixel art
      • AABB collision
      • Sprite
      • Game loop
  • Education
    • Tools
      • Anki
  • Electronic
    • Components
      • Opto-isolator
  • Image quality
  • Languages
    • English
  • Utilities
    • Docker
    • SSH
  • Lifestyle
    • Minimalism
    • Reading
  • Science
    • Math
      • Fractions
  • Productivity
    • Bullet journaling
    • Typing
  • Operating system
    • Unix
    • Linux
    • MacOS
  • Learning resources
  • Research
Powered by GitBook
On this page
  • Sort array
  • Bubble sort

Was this helpful?

  1. Development
  2. Programming languages
  3. Javascript

Arrays

Sort array

const numbers = [10, 7, 2901, 3.5];
numbers.sort((a, b) => a - b); // ascending

Bubble sort

const bubbleSort = arr => { let sorted;
  do { sorted = false;
    for (let i = 0; i < arr.length - 1; i += 1) {
      if (arr[i] > arr[i + 1]) {
        const temp = arr[i]
    
        arr[i] = arr[i + 1]
        arr[i + 1] = temp;
    
        sorted = true
      }
    }
  } while (sorted)
  return arr;
}

PreviousJavascriptNextJSDoc

Last updated 4 years ago

Was this helpful?