Snippet

Trier l'objet de tableau dans React

2 réponses de la communauté

const list = [
  { color: 'white', size: 'XXL' },
  { color: 'red', size: 'XL' },
  { color: 'black', size: 'M' }
]

list.sort((a, b) => (a.color > b.color) ? 1 : -1)
					
const[dataValue, setDataValue] = useState([]);                    
              const filter = () => {
                    let datafil = filterdataArray; // Array Filter Data Here
+                    let val = datafil.sort(function (a, b) {
+                      let dateA = a.name.toLowerCase();
+                      let dateB = b.name.toLowerCase();
+                      if (dateA < dateB) {
+                        return -1;
+                      } else if (dateA > dateB) {
+                        return 1;
+                      }
+                      return 0;
+                    });
+                    setDataValue(val);
	   }