Skip to main content
minus 0.5 and equal comparison without type check
Source Link

JavaScript, 9898 92 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>b=r=>{for(;!r.every((e,o,t)=>e===[=>o==[...r].sort()[o][t]);)r.sort(()=>Math.random()>-.5?1:-1);return r};

Invocation: console.log(fb([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v ===== [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > -0.5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array destructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind

JavaScript, 98 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>{for(;!r.every((e,o)=>e===[...r].sort()[o]);)r.sort(()=>Math.random()>.5?1:-1);return r};

Invocation: console.log(f([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v === [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > .5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array destructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind

JavaScript, 98 92 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let b=r=>{for(;!r.every((o,t)=>o==[...r].sort()[t]);)r.sort(()=>Math.random()-.5);return r};

Invocation: console.log(b([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v == [...myArray].sort()[i])) {      
    myArray.sort(() => Math.random() -0.5)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array destructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind
fixes typo and adds link
Source Link

JavaScript, 98 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>{for(;!r.every((e,o)=>e===[...r].sort()[o]);)r.sort(()=>Math.random()>.5?1:-1);return r};

Invocation: console.log(f([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v === [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > .5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array restructuringarray destructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind

JavaScript, 98 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>{for(;!r.every((e,o)=>e===[...r].sort()[o]);)r.sort(()=>Math.random()>.5?1:-1);return r};

Invocation: console.log(f([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v === [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > .5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array restructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind

JavaScript, 98 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>{for(;!r.every((e,o)=>e===[...r].sort()[o]);)r.sort(()=>Math.random()>.5?1:-1);return r};

Invocation: console.log(f([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v === [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > .5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array destructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind
Source Link

JavaScript, 98 chars

More precisely ECMAScript 2015 (ES6)

Minified:

let f=r=>{for(;!r.every((e,o)=>e===[...r].sort()[o]);)r.sort(()=>Math.random()>.5?1:-1);return r};

Invocation: console.log(f([3, 2, 1, 5, 67, 4, 6, 5, 6, 4]))

Not minified:

let bogoSort = (myArray) => {
  while (!myArray.every((v, i) => v === [...myArray].sort()[i])) {      
    myArray.sort(() => (Math.random() > .5) ? 1 : -1)    
  }
  return myArray
}

Explanation:

  • myArray.every() is an element-wise compare function. Comparing shuffled with sorted array
  • someArray.sort() sorts in place so saving assignments, but also needs cloning for the sorted array
  • [...myArray] clones the array (array restructuring)
  • () => (Math.random() > .5) ? 1 : -1 is a custom compare function. Coin flip whether to order an element in front or behind