GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing fr.javascript.info — Copier et trier le tableau

  • Tutoriel
  • JavaScript le langage
  • Types de données
  • Méthodes de tableau
retour au cours

Copier et trier le tableau

importance: 5

Nous avons un tableau de chaînes arr. Nous aimerions en avoir une copie triée, mais sans modifier arr.

Créez une fonction copySorted(arr) qui renvoie une copie triée.

let arr = ["HTML", "JavaScript", "CSS"];

let sorted = copySorted(arr);

alert( sorted ); // CSS, HTML, JavaScript
alert( arr ); // HTML, JavaScript, CSS (aucune modification)

Nous pouvons utiliser slice() pour faire une copie et exécuter le tri sur celle-ci :

function copySorted(arr) {
  return arr.slice().sort();
}

let arr = ["HTML", "JavaScript", "CSS"];

let sorted = copySorted(arr);

alert( sorted );
alert( arr );
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.