From 932b93b9c71d22c65a956fb27c38719e0d5ee116 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Thu, 11 Apr 2019 19:20:24 +0200 Subject: [PATCH] Alpha sort for search --- middleware/Vegetables.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/middleware/Vegetables.js b/middleware/Vegetables.js index 6167e55..20b0c71 100644 --- a/middleware/Vegetables.js +++ b/middleware/Vegetables.js @@ -174,7 +174,22 @@ class Vegetables { callback(new Error('No vegetable found'), 204) return false } - callback(null, items) + + const _items = items.rows.toJSON() + let vegetables = [] + + for (let j = 0; j < _items.length; j += 1) { + let k = 0 + for (k = 0; k < vegetables.length; k += 1) { + if (_items[j].name.localeCompare(vegetables[k].name) === -1) { + break + } + } + + vegetables.splice(k, 0, _items[j]) + } + + callback(null, { count: vegetables.length, rows: vegetables }) }) .catch((e) => { callback(e, null)