logo

كيفية فرز الأحرف في سلسلة في جافا سكريبت

فرز الشخصيات في سلسلة هي مهمة شائعة في البرمجة، وخاصة في تطوير الويب. في JavaScript، هناك طرق مختلفة لفرز الأحرف في سلسلة. في هذه المقالة، سوف نستكشف بعض الأساليب الأكثر شيوعًا لفرز الأحرف في سلسلة في JavaScript.

Iskcon النموذج الكامل

فرز الأحرف في سلسلة باستخدام طريقة Array.sort() :

أسهل طريقة لفرز الأحرف في سلسلة في JavaScript هي تحويل السلسلة إلى مصفوفة من الأحرف ثم استخدام الأمر صفيف.فرز () طريقة فرز المصفوفة.

مثال:

يوضح التعليمة البرمجية التالية كيفية فرز الأحرف في سلسلة باستخدام هذه الطريقة:

 const str = 'hello world'; const sortedStr = str.split('').sort().join(''); console.log(sortedStr); 

انتاج:

 dehllloorw 

توضيح:

في هذا الكود، نقوم أولاً بإنشاء سلسلة شارع ثم قم بتحويله إلى مجموعة من الأحرف باستخدام ملف ينقسم() طريقة. بعد ذلك نستخدم طريقة الفرز (). لفرز الأحرف في المصفوفة بترتيب تصاعدي. وأخيرًا، نقوم بضم المصفوفة التي تم فرزها مرة أخرى إلى سلسلة باستخدام التابع ينضم() طريقة.

نلاحظ أن نوع() تقوم الطريقة بفرز العناصر في مكانها، مما يعني أنها تعدل المصفوفة الأصلية. في المثال أعلاه، نحن لا نحتفظ بالسلسلة الأصلية لأننا نقوم بتعديلها مباشرة. إذا أردنا الحفاظ على السلسلة الأصلية، يمكننا عمل نسخة منها قبل تحويلها إلى مصفوفة:

مثال:

 const str = 'hello world'; const strCopy = str.slice(); // make a copy of the string const sortedStr = strCopy.split('').sort().join(''); console.log(sortedStr); 

انتاج:

 dehllloorw 

فرز الأحرف في سلسلة باستخدام حلقة for:

هناك طريقة أخرى لفرز الأحرف في سلسلة في JavaScript وهي باستخدام ملف لحلقة . تتضمن هذه الطريقة التكرار على كل حرف في السلسلة، ومقارنتها بكل حرف آخر، وتبديل مواضعها إذا لم تكن بالترتيب الصحيح.

مثال:

فيما يلي مثال لكيفية فرز الأحرف في سلسلة باستخدام حلقة for:

 const str = &apos;hello world&apos;; let sortedStr = &apos;&apos;; for (let i = 0; i <str.length; i++) { for (let j="i" + 1; < str.length; j++) if (str[j] str[i]) const temp="str[i];" str[i]="str[j];" str[j]="temp;" } sortedstr console.log(sortedstr); pre> <p> <strong>Output:</strong> </p> <pre> hello world </pre> <p> <strong>Explanation:</strong> </p> <p>In this code, we first initialize an empty string called <strong> <em>sortedStr</em> </strong> . After that, we use two nested <strong> <em>for loops</em> </strong> to compare each character with every other character in the string. If a character is not in the correct order, we swap it with the character that comes after it.</p> <p>After the <strong> <em>inner loop completes</em> </strong> , we add the current character to the <strong> <em>sortedStr</em> </strong> string. We continue this process until all characters have been sorted. This method may be less efficient than using the <strong> <em>Array.sort()</em> </strong> method, especially for larger strings. However, it can be useful for understanding the sorting process and for implementing custom sorting algorithms.</p> <h3>Sorting characters in a string using a library:</h3> <p>There are also several JavaScript libraries that provide sorting functions for strings. One popular library is <strong> <em>lodash</em> </strong> , which provides a <strong> <em>sortBy()</em> </strong> function that can be used to sort characters in a string:</p> <p> <strong>Example:</strong> </p> <pre> const _ = require(&apos;lodash&apos;); const str = &apos;hello world&apos;; const sortedStr = _.sortBy(str).join(&apos;&apos;); console.log(sortedStr); </pre> <p> <strong>Output:</strong> </p> <pre> dehllloorw </pre> <p> <strong>Explanation:</strong> </p> <p>In this code, we first <strong> <em>import</em> </strong> the <strong> <em>lodash</em> </strong> library using the <strong> <em>require()</em> </strong> function. After that, we use the <strong> <em>sortBy()</em> </strong> function to sort the characters in the string in ascending order. Finally, we join the sorted array back into a string using the <strong> <em>join()</em> </strong> method.</p> <h4>Note that:- we can also use the <em>spread operator (...)</em> to convert the string into an array without using the <em>split() method</em> :</h4> <pre> const _ = require(&apos;lodash&apos;); const str = &apos;hello world&apos;; const sortedStr = _.sortBy([...str]).join(&apos;&apos;); console.log(sortedStr); </pre> <p> <strong>Output:</strong> </p> <pre> dehllloorw </pre> <h3>Sorting characters in descending order:</h3> <p>By default, the <strong> <em>Array.sort()</em> </strong> method sorts elements in ascending order. However, we can sort elements in descending order by passing a comparison function to the <strong> <em>sort() method</em> </strong> .</p> <p> <strong>Example:</strong> </p> <p>Here&apos;s an example of how to sort characters in a string in descending order:</p> <pre> const str = &apos;hello world&apos;; const sortedStr = str.split(&apos;&apos;).sort((a, b) =&gt; b.localeCompare(a)).join(&apos;&apos;); console.log(sortedStr); </pre> <p> <strong>Output:</strong> </p> <pre> wroolllhed </pre> <p> <strong>Explanation:</strong> </p> <p>In this code, we pass a comparison function to the <strong> <em>sort() method</em> </strong> that compares characters in descending order using the <strong> <em>localeCompare()</em> </strong> method.</p> <h2>Conclusion:</h2> <p>Sorting characters in a string is a common task in JavaScript programming. We can use several techniques to achieve this, including the <strong> <em>Array.sort() method</em> </strong> , a <strong> <em>for loop</em> </strong> , or a <strong> <em>library function</em> </strong> . The most suitable method depends on the specific requirements of the task and the size of the input string.</p> <hr></str.length;>

توضيح:

في هذا الكود، نقوم أولاً بتهيئة سلسلة فارغة تسمى فرزها . بعد ذلك نستخدم اثنين متداخلين للحلقات لمقارنة كل حرف مع كل حرف آخر في السلسلة. إذا لم يكن الحرف بالترتيب الصحيح، فإننا نستبدله بالحرف الذي يأتي بعده.

بعد تكتمل الحلقة الداخلية ، نضيف الحرف الحالي إلى فرزها خيط. نواصل هذه العملية حتى يتم فرز كافة الأحرف. قد تكون هذه الطريقة أقل كفاءة من استخدام صفيف.فرز () الطريقة، وخاصة بالنسبة للسلاسل الكبيرة. ومع ذلك، يمكن أن يكون مفيدًا لفهم عملية الفرز وتنفيذ خوارزميات الفرز المخصصة.

فرز الأحرف في سلسلة باستخدام مكتبة:

هناك أيضًا العديد من مكتبات JavaScript التي توفر وظائف فرز للسلاسل. إحدى المكتبات الشعبية هي لوداش ، والذي يوفر أ ترتيب حسب() دالة يمكن استخدامها لفرز الأحرف في سلسلة:

مثال:

 const _ = require(&apos;lodash&apos;); const str = &apos;hello world&apos;; const sortedStr = _.sortBy(str).join(&apos;&apos;); console.log(sortedStr); 

انتاج:

 dehllloorw 

توضيح:

في هذا الرمز، نحن أولا يستورد ال لوداش المكتبة باستخدام يتطلب() وظيفة. بعد ذلك نستخدم ترتيب حسب() وظيفة لفرز الأحرف في السلسلة بترتيب تصاعدي. وأخيرًا، نقوم بضم المصفوفة التي تم فرزها مرة أخرى إلى سلسلة باستخدام التابع ينضم() طريقة.

ميزات جافا 8

لاحظ أن :- يمكننا أيضًا استخدام عامل الانتشار (...) لتحويل السلسلة إلى صفيف دون استخدام طريقة التقسيم :

 const _ = require(&apos;lodash&apos;); const str = &apos;hello world&apos;; const sortedStr = _.sortBy([...str]).join(&apos;&apos;); console.log(sortedStr); 

انتاج:

 dehllloorw 

ترتيب الحروف تنازليا:

بشكل افتراضي، صفيف.فرز () تقوم الطريقة بفرز العناصر بترتيب تصاعدي. ومع ذلك، يمكننا فرز العناصر بترتيب تنازلي عن طريق تمرير دالة المقارنة إلى طريقة الفرز (). .

مثال:

فيما يلي مثال لكيفية فرز الأحرف في سلسلة بترتيب تنازلي:

 const str = &apos;hello world&apos;; const sortedStr = str.split(&apos;&apos;).sort((a, b) =&gt; b.localeCompare(a)).join(&apos;&apos;); console.log(sortedStr); 

انتاج:

 wroolllhed 

توضيح:

في هذا الكود نقوم بتمرير دالة مقارنة إلى طريقة الفرز (). الذي يقارن الأحرف بترتيب تنازلي باستخدام مقارنة اللغة () طريقة.

خاتمة:

يعد فرز الأحرف في سلسلة مهمة شائعة في برمجة JavaScript. يمكننا استخدام عدة تقنيات لتحقيق ذلك، بما في ذلك طريقة Array.sort() ، أ لحلقة ، أو أ وظيفة المكتبة . تعتمد الطريقة الأكثر ملاءمة على المتطلبات المحددة للمهمة وحجم سلسلة الإدخال.