मेरी परियोजना में, मुझे एक सरणी को सॉर्ट करने की आवश्यकता है जिसमें किसी अन्य सरणी (यह आइटम) की अनुक्रमणिका है। मैंने कई घंटों तक खोज की है, लेकिन मुझे अपनी समस्या वाला कोई नहीं मिला।
var arr = [1, 4, 3, 4, 5, 6, 7, 8, 9];
function sorting(){
let arr2 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
//sorting code
}
अब, मैं एआर 2 को सॉर्ट करना चाहता हूं, इसलिए जब मैं इस तरह के कोड (इस अनुच्छेद के तहत) के साथ लूप करता हूं, तो मैं क्रमबद्ध सरणी (एआर 2) में इंडेक्स के साथ एआर एक्सेस करता हूं।
arr[arr2[i]]
मेरा पहला कदम arr2.sort(function(a,b){arr[a] - arr[b]} का उपयोग करना था, लेकिन हर बार सॉर्ट अच्छा नहीं था। मैं अपना खुद का सॉर्टिंग फ़ंक्शन बनाने का प्रयास करता हूं, लेकिन मेरी समस्या रुके।
संक्षेप में, मैं एआर 2 को सॉर्ट करना चाहता हूं, इसलिए जब मैं इसके माध्यम से लूप करता हूं, तो मुझे आरोही (या अवरोही) क्रम में एआर का मान मिलता है।
संपादित करें मैं इस समस्या को ठीक करता हूं, लेकिन दूसरा प्रकट होता है, जब मैंने अपने html पर arr2 लागू किया, तो ऑर्डर गड़बड़ हो गया।
var arr = [1, 4, 3, 4, 5, 6, 7, 8, 9];
function sorting(){
let arr2 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
//The sorting block code (done)
z = document.getElementsByClassName("triable"); //this is on what I applied arr2
for (let i = 0; i < z.length; i++){
z[i].style.order = arr2[i]; //this line work, but doesn't correctly do what I what it to do
}
}
एचटीएमएल के लिए, मेरे पास "ट्रायबल" वर्ग के साथ कुछ div है और इसके ऊपर के कोड को एक सीएसएस शैली (ऑर्डर) लागू करने की आवश्यकता है ताकि div दृष्टि से स्थिति बदल सके
4 जवाब
आपको डेल्टा वापस करने की आवश्यकता है। अन्यथा कॉलबैक प्रत्येक कॉल के लिए undefined
लौटाता है।
arr2.sort(function(a, b) {
return arr[b] - arr[a];
});
सही क्रम जोड़ने के लिए, आपको सही तत्व को संबोधित करने के लिए indices
से इंडेक्स लेना होगा और i
को स्टाइल ऑर्डर वैल्यू के रूप में असाइन करना होगा।
function sort() {
var array = [1, 4, 3, 4, 5, 6, 7, 8, 9],
z = document.getElementsByClassName("triable");
[...array.keys()]
.sort((a, b) => array[b] - array[a])
.forEach((v, i) => z[v].style.order = i);
}
<button onclick="sort()">sort</button><br>
<div style="display: flex;">
<span class="triable">1</span>
<span class="triable">4</span>
<span class="triable">3</span>
<span class="triable">4</span>
<span class="triable">5</span>
<span class="triable">6</span>
<span class="triable">7</span>
<span class="triable">8</span>
<span class="triable">9</span>
</div>
a
और b
का आदान-प्रदान करें।
बहुत कोड, लेकिन यह काम करता है :)
एएससी क्रमित करने के लिए:
var test = [1, 4, 3, 4, 5, 6, 7, 8, 9];
console.log("Original Array: " + test);
var len = test.length;
var indices = new Array(len);
for (var i = 0; i < len; ++i) indices[i] = i;
indices.sort(function (a, b) { return test[a] < test[b] ? -1 : test[a] > test[b] ? 1 : 0; });
test.sort();
console.log("Sort-ASC " + test);
console.log("Index from Array " + indices);
डीईएससी क्रमित करने के लिए:
var test = [1, 4, 3, 4, 5, 6, 7, 8, 9];
console.log("Originales Array: " + test)
var len = test.length;
var indices = new Array(len);
for (var i = 0; i < len; ++i) indices[i] = i;
indices.sort(function (a, b) { return test[a] < test[b] ? -1 : test[a] > test[b] ? 1 : 0; });
indices.reverse();
test.sort();
test.reverse();
console.log("Sort-DESC " + test);
console.log("Index from Array " + indices);
कार्यात्मक मनमाना क्रम
अपनी समस्या से संपर्क करने का एक और तरीका यहां दिया गया है। मान लें कि हमारे पास कुछ fruits
हैं और एक मनमाना order
हम उन्हें क्रम से लगाना चाहते हैं -
const fruits =
// 0 1 2 3 4
[ "apple", "banana", "cherry", "orange", "peach" ]
const order =
[ 1, 3, 2, 0, 4 ]
हम कुछ इस तरह लिखने में सक्षम होना चाहते हैं -
fruits.sort(sortByIndex(fruits, order))
console.log(fruits)
// [ "banana", "orange", "cherry", "apple", "peach" ]
// 1 3 2 0 4
हम अपने छँटाई कोड को संभालने के लिए एक Comparison
मॉड्यूल की कामना करते हैं -
const { empty, map } =
Comparison
const sortByIndex = (values = [], indexes = []) =>
map(empty, x => indexes.indexOf(values.indexOf(x)))
अब हमें केवल Comparison
लागू करना है -
const Comparison =
{ empty: (a, b) =>
a < b ? -1
: a > b ? 1
: 0
, map: (m, f) =>
(a, b) => m(f(a), f(b))
}
const { empty, map } =
Comparison
const sortByIndex = (values = [], indexes = []) =>
map(empty, x => indexes.indexOf(values.indexOf(x)))
const fruits =
[ "apple", "banana", "cherry", "orange", "peach" ]
// 0 1 2 3 4
const order =
[ 1, 3, 2, 0, 4 ]
console.log(fruits)
// [ "apple", "banana", "cherry", "orange", "peach" ]
console.log(fruits.sort(sortByIndex(fruits, order)))
// [ "banana", "orange", "cherry", "apple", "peach" ]
मॉड्यूल क्यों?
Comparison
मॉड्यूल को लागू करने का मतलब है कि हमारे पास अपने सभी तुलनात्मक तर्क को स्टोर करने के लिए एक साफ जगह है। हम अब reverse
और concat
जैसे अन्य उपयोगी कार्यों को आसानी से लागू कर सकते हैं -
const Comparison =
{ // ...
, concat: (m, n) =>
(a, b) => Ordered.concat(m(a, b), n(a, b))
, reverse: (m) =>
(a, b) => m(b, a)
}
const Ordered =
{ empty: 0
, concat: (a, b) =>
a === 0 ? b : a
}
अब हम जटिल छँटाई तर्क को आसानी से मॉडल कर सकते हैं -
const sortByName =
map(empty, x => x.name)
const sortByAge =
map(empty, x => x.age)
const data =
[ { name: 'Alicia', age: 10 }
, { name: 'Alice', age: 15 }
, { name: 'Alice', age: 10 }
, { name: 'Alice', age: 16 }
]
name
के आधार पर छाँटें और फिर age
के अनुसार क्रमित करें -
data.sort(concat(sortByName, sortByAge))
// [ { name: 'Alice', age: 10 }
// , { name: 'Alice', age: 15 }
// , { name: 'Alice', age: 16 }
// , { name: 'Alicia', age: 10 }
// ]
age
के आधार पर छाँटें और फिर name
के अनुसार क्रमित करें -
data.sort(concat(sortByAge, sortByName))
// [ { name: 'Alice', age: 10 }
// , { name: 'Alicia', age: 10 }
// , { name: 'Alice', age: 15 }
// , { name: 'Alice', age: 16 }
// ]
और आसानी से reverse
कोई भी सॉर्टर। यहां हम name
के आधार पर क्रमित करते हैं और फिर age
के आधार पर क्रम से लगाते हैं -
data.sort(concat(sortByName, reverse(sortByAge)))
// [ { name: 'Alice', age: 16 }
// , { name: 'Alice', age: 15 }
// , { name: 'Alice', age: 10 }
// , { name: 'Alicia', age: 10 }
// ]
कार्यात्मक सिद्धांत
हमारा Comparison
मॉड्यूल लचीला है फिर भी विश्वसनीय है। यह हमें अपने सॉर्टर्स को सूत्र की तरह लिखने की अनुमति देता है -
// this...
concat(reverse(sortByName), reverse(sortByAge))
// is the same as...
reverse(concat(sortByName, sortByAge))
और इसी तरह concat
भावों के साथ -
// this...
concat(sortByYear, concat(sortByMonth, sortByDay))
// is the same as...
concat(concat(sortByYear, sortByMonth), sortByDay)
// is the same as...
nsort(sortByYear, sortByMonth, sortByDay)
nsort
के साथ पागल हो जाएं
अब मान लीजिए कि हम कारकों की एक मनमानी संख्या के आधार पर छाँटना चाहते हैं। उदाहरण के लिए, दिनांक वस्तुओं को क्रमबद्ध करने के लिए तीन तुलनाओं की आवश्यकता होती है: year
, month
, और day
-
const { empty, map, reverse, nsort } =
Comparison
const data =
[ { year: 2020, month: 4, day: 5 }
, { year: 2018, month: 1, day: 20 }
, { year: 2019, month: 3, day: 14 }
]
const sortByDate =
nsort
( map(empty, x => x.year) // primary: sort by year
, map(empty, x => x.month) // secondary: sort by month
, map(empty, x => x.day) // tertiary: sort by day
)
अब हम year
, month
, day
द्वारा क्रमबद्ध कर सकते हैं -
data.sort(sortByDate)
// [ { year: 2019, month: 11, day: 14 }
// , { year: 2020, month: 4, day: 3 }
// , { year: 2020, month: 4, day: 5 }
// ]
और year
, month
, day
द्वारा आसानी से रिवर्स सॉर्ट करें -
data.sort(reverse(sortByDate))
// [ { year: 2020, month: 4, day: 5 }
// , { year: 2020, month: 4, day: 3 }
// , { year: 2019, month: 11, day: 14 }
// ]
कार्यात्मक सिद्धांतों के लिए एन-सॉर्ट को लागू करना एक हवा है। हमारे concat
और empty
पूरी मेहनत करते हैं -
const Comparison =
{ // ...
, nsort: (...m) =>
m.reduce(Comparison.concat, Comparison.empty)
}
इस कोड को कार्य में देखने के लिए नीचे दिए गए स्निपेट का विस्तार करें -
const Comparison =
{ empty: (a, b) =>
a < b ? -1
: a > b ? 1
: 0
, map: (m, f) =>
(a, b) => m(f(a), f(b))
, concat: (m, n) =>
(a, b) => Ordered.concat(m(a, b), n(a, b))
, reverse: (m) =>
(a, b) => m(b, a)
, nsort: (...m) =>
m.reduce(Comparison.concat, Comparison.empty)
}
const Ordered =
{ empty: 0
, concat: (a, b) =>
a === 0 ? b : a
}
const { empty, map, concat, reverse, nsort } =
Comparison
const sortByDate =
nsort
( map(empty, x => x.year) // primary
, map(empty, x => x.month) // secondary
, map(empty, x => x.day) // tertiary
)
const data =
[ { year: 2020, month: 4, day: 5 }
, { year: 2019, month: 11, day: 14 }
, { year: 2020, month: 4, day: 3 }
]
console.log(data.sort(reverse(sortByDate)))
// [ { year: 2020, month: 4, day: 5 }
// , { year: 2020, month: 4, day: 3 }
// , { year: 2019, month: 11, day: 14 }
// ]
जावास्क्रिप्ट मॉड्यूल
ऊपर Comparison
और Ordered
को साधारण वस्तुओं के रूप में परिभाषित किया गया है। जावास्क्रिप्ट एक बहुत ही लचीली भाषा है और import
/export
सिंटेक्स आपके प्रोग्राम को मॉडर्नाइज करने के लिए स्पष्ट रूप से उपलब्ध कराए गए थे। इस तरह से मॉड्यूल लिखने से हमें एक स्पष्ट तस्वीर मिलती है कि चीजों को कहाँ जाना चाहिए और हमें अपना कोड विकसित करने के लिए बहुत जगह प्रदान करता है -
// Comparison.js
import { lt, gt, eq, concat:_concat } from "./Ordered"
const asc = (a, b) =>
(console.log(a, b), a < b) ? lt
: a > b ? gt
: eq
const empty =
asc
const map = (m, f) =>
(a, b) => m(f(a), f(b))
const concat = (m, n) =>
(a, b) => _concat(m(a, b), n(a, b))
const reverse = (m) =>
(a, b) => m(b, a)
const desc =
reverse(asc)
export { asc, concat, desc, empty, map, reverse }
// Ordered.js
const lt =
-1
const gt =
1
const eq =
0
const empty =
eq
const concat = (a, b) =>
a === eq ? b : a
export { concat, empty, eq, gt, lt }
इसका बहुत आसान दोस्त इस arr.sort (फ़ंक्शन (ए, बी) {रिटर्न ए - बी}) का उपयोग करता है; // वापसी का उपयोग करें अन्यथा यह ट्रिगर नहीं होगा और - + प्लस ascndng dcndng ordee सरल विधि को दर्शाता है ..... हैप्पी कोडिंग