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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 63: سطر 63:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
===أنظر أيضًا===
+
==أنظر أيضًا==
 
*<code>[[Kotlin/collections/sortBy|sortBy()]]</code>‎ : تقوم بترتيب  [[Kotlin/Array|المصفوفة]] أو [[Kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة . 
 
*<code>[[Kotlin/collections/sortBy|sortBy()]]</code>‎ : تقوم بترتيب  [[Kotlin/Array|المصفوفة]] أو [[Kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة . 
 
==مصادر==
 
==مصادر==

مراجعة 17:45، 6 يونيو 2018

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

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

في حال استدعاء الدالة  slice()‎ عبر مصفوفة فستقوم بترتيبها:

fun IntArray.sort() 
fun LongArray.sort() 
fun ByteArray.sort() 
fun ShortArray.sort() 
fun DoubleArray.sort() 
fun FloatArray.sort() 
fun CharArray.sort() 
inline fun <T : Comparable<T>> Array<out T>.sort() (source)

في حال استدعاء الدالة  slice()‎ عبر مصفوفة مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل المصفوفة:

fun <T> Array<out T>.sort(
    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)

بيئة التشغيل: JVM في حال استدعاء الدالة  slice()‎ عبر كائن من النوع MutableList فستقوم بترتيبه:

fun <T : Comparable<T>> MutableList<T>.sort()

في حال استدعاء الدالة  slice()‎ عبر مصفوفة مع تمرير مًقارن comparison كمعامل فستقوم بترتيب تلك المصفوفة بحسب الترتيب الذي يحدده المقارن comparison:

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)

يُلاحَظ وجود الكلمة المفتاحية inline للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع توثيق الدوال المباشرة (inline functions).

أمثلة

استخدام الدالة ()sort مع المصفوفات

تعرف الشيفرة الآتية  مصفوفةباسم array مكونة من ستة عناصر باستخدام الدالة ()arrayOf‎، ثم تستخدم الدالة()sort  على array لترتيبه، ثم تطبع الناتج:

import java.util.Arrays

fun main(args: Array<String>) {
val array = arrayOf(1, 9, 3, 7, 5, 6)

array.sort()
    
print (Arrays.toString(array)) // [1, 3, 5, 6, 7, 9]
}

أنظر أيضًا

مصادر