الفرق بين المراجعتين ل"Kotlin/collections/sorted"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في لغة Kotlin}}</noinclude> تقوم الدالة<code>sorted()‎</code> بترتيب Kotlin/Ar...')
 
ط
 
(5 مراجعات متوسطة بواسطة 3 مستخدمين غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في لغة Kotlin}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في Kotlin}}</noinclude>
تقوم الدالة<code>sorted()‎</code> بترتيب  [[Kotlin/Array|المصفوفة]] أو [[kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها.  
+
ترتِّب الدالة <code>sorted()‎</code> جميع عناصر [[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت معها تصاعديًّا ثمَّ تضع الناتج في [[Kotlin/List|قائمة]].  
  
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] فستقوم بترتيبها: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
fun IntArray.sort()  
+
fun <T : Comparable<T>> Array<out T>.sorted(): List<T>
fun LongArray.sort()  
+
fun ByteArray.sorted(): List<Byte>
fun ByteArray.sort()  
+
fun ShortArray.sorted(): List<Short>
fun ShortArray.sort()  
+
fun IntArray.sorted(): List<Int>
fun DoubleArray.sort()  
+
fun LongArray.sorted(): List<Long>
fun FloatArray.sort()  
+
fun FloatArray.sorted(): List<Float>
fun CharArray.sort()  
+
fun DoubleArray.sorted(): List<Double>
inline fun <T : Comparable<T>> Array<out T>.sort() (source)
+
fun CharArray.sorted(): List<Char>
 +
fun <T : Comparable<T>> Iterable<T>.sorted(): List<T>
 +
</syntaxhighlight>
  
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل [[Kotlin/Array|المصفوفة]]: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
== القيمة المعادة ==
fun <T> Array<out T>.sort(
+
تُعاد [[Kotlin/List|قائمة]] تضم عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] المعطاة بعد ترتيبها تصاعديًّا.
    fromIndex: Int = 0,
 
    toIndex: Int = size)
 
fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
 
 
</syntaxhighlight>'''بيئة التشغيل: JVM'''
 
 
 
في حال استدعاء الدالة  <code>sort()‎</code> عبر  كائن من النوع <code>[[kotlin/MutableList|MutableList]]</code> فستقوم بترتيبه:<syntaxhighlight lang="kotlin">
 
fun <T : Comparable<T>> MutableList<T>.sort()
 
 
 
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير مًقارن <code>comparison</code> كمعامل فستقوم بترتيب تلك [[Kotlin/Array|المصفوفة]] بحسب الترتيب الذي يحدده المقارن <code>comparison</code>: <syntaxhighlight lang="kotlin">
 
inline fun <T> Array<out T>.sort(
 
    noinline comparison: (a: T, b: T) -> Int)
 
inline fun ByteArray.sort(
 
    noinline comparison: (a: Byte, b: Byte) -> Int)
 
inline fun ShortArray.sort(
 
    noinline comparison: (a: Short, b: Short) -> Int)
 
inline fun IntArray.sort(
 
    noinline comparison: (a: Int, b: Int) -> Int)
 
inline fun LongArray.sort(
 
    noinline comparison: (a: Long, b: Long) -> Int)
 
inline fun FloatArray.sort(
 
    noinline comparison: (a: Float, b: Float) -> Int)
 
inline fun DoubleArray.sort(
 
    noinline comparison: (a: Double, b: Double) -> Int)
 
inline fun CharArray.sort(
 
    noinline comparison: (a: Char, b: Char) -> Int)
 
 
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
  
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()sorted</code> مع المصفوفات===
+
<nowiki/><nowiki/>استعمال الدالة <code>()sorted</code> لترتيب عناصر مصفوفة تصاعديًّا:<syntaxhighlight lang="kotlin">
تعرف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]]<nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf‎</code>، ثم تستخدم الدالة<code>()sorted</code>  على <code>array</code> لترتيبه، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
import java.util.Arrays
 
 
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
val array = arrayOf(1, 9, 3, 7, 5, 6)
+
    val array = arrayOf(1, 9, -3, 7, 5, 6)
 
 
array.sort()
 
 
      
 
      
print (Arrays.toString(array)) // [1, 3, 5, 6, 7, 9]
+
    print(array.sorted()) // [-3, 1, 5, 6, 7, 9]
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
==أنظر أيضًا==
+
==انظر أيضًا==
*<code>[[Kotlin/collections/sortBy|sortBy()]]</code>‎ : تقوم بترتيب  [[Kotlin/Array|المصفوفة]] أو [[Kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة . 
+
*الدالة <code>[[Kotlin/collections/sortedArray|sortedArray()]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] التي استُدعيت معها تصاعديًّا ثمَّ تضع الناتج في [[Kotlin/List|مصفوفة]] جديدة.
 +
*الدالة <code>[[Kotlin/collections/sortedArrayDescending|sortedArrayDescending()]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] التي استُدعيت معها تنازليًّا ثمَّ تضع الناتج في [[Kotlin/List|مصفوفة]] جديدة.
 +
*الدالة <code>[[Kotlin/collections/sortedArrayWith|sortedArrayWith()‎]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] التي استُدعيت معها وفقًا للمُقارِن المُمرّر إليها ثمَّ تضع الناتج في مصفوفة جديدة.
 +
*الدالة <code>[[Kotlin/collections/sortedBy|sortedBy()]]</code>‎: عناصر <nowiki/>[[Kotlin/Array|المصفوفة]] أو القائمة المتغيرة (MutableList) التي استُدعيت معها تصاعديًّا وفقًا للقيم التي تعيدها الدالة المُمرّرة إليها عند تطبيقها على كل عنصر.
 +
*الدالة <code>[[Kotlin/collections/sortedByDescending|sortedByDescending()‎]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/Iterable|المجموعة التكرارية]] (Iterable) التي استُدعيت معها تنازليًّا وفقًا للقيم التي تعيدها الدالة المُمرّرة إليها عند تطبيقها على كل عنصر ثمَّ تضع الناتج في قائمة
 +
*الدالة <code>[[Kotlin/collections/sortedDescending|sortedDescending()‎]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت معها تنازليًّا ثمَّ تضع الناتج في [[Kotlin/List|قائمة]].
 +
*الدالة <code>[[Kotlin/collections/sortedWith|sortedWith()‎]]</code>: ترتِّب جميع عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت معها وفقًا للمُقارِن المُمرّر إليها ثمَّ تضع الناتج في قائمة.
 +
 
 +
* الدالة <code>[[Kotlin/collections/sort|sort()]]</code>‎ : ترتب <nowiki/>[[Kotlin/Array|المصفوفة]] أو القائمة المتغيرة (MutableList) التي استُدعيت معها تصاعديًّا.
 +
 
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort.html الدالة  sorted()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted.html صفحة الدالة sorted()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Functions]]
+
[[تصنيف:Kotlin Function]]
 +
[[تصنيف:Kotlin Collection]]

المراجعة الحالية بتاريخ 11:12، 8 سبتمبر 2018

ترتِّب الدالة sorted()‎ جميع عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت معها تصاعديًّا ثمَّ تضع الناتج في قائمة.

البنية العامة

fun <T : Comparable<T>> Array<out T>.sorted(): List<T> 
fun ByteArray.sorted(): List<Byte> 
fun ShortArray.sorted(): List<Short> 
fun IntArray.sorted(): List<Int> 
fun LongArray.sorted(): List<Long> 
fun FloatArray.sorted(): List<Float> 
fun DoubleArray.sorted(): List<Double> 
fun CharArray.sorted(): List<Char> 
fun <T : Comparable<T>> Iterable<T>.sorted(): List<T>

القيمة المعادة

تُعاد قائمة تضم عناصر المصفوفة أو المجموعة التكرارية المعطاة بعد ترتيبها تصاعديًّا.

أمثلة

استعمال الدالة ()sorted لترتيب عناصر مصفوفة تصاعديًّا:

fun main(args: Array<String>) {
    val array = arrayOf(1, 9, -3, 7, 5, 6)
    
    print(array.sorted()) // [-3, 1, 5, 6, 7, 9]
}

انظر أيضًا

  • الدالة sort()‎ : ترتب المصفوفة أو القائمة المتغيرة (MutableList) التي استُدعيت معها تصاعديًّا.

مصادر