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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
ط (إضافة دوال إلى قسم "انظر أيضًا")
ط (استبدال النص - 'Kotlin Functions' ب'Kotlin Function')
سطر 67: سطر 67:
 
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort.html صفحة الدالة sort()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort.html صفحة الدالة sort()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Functions]]
+
[[تصنيف:Kotlin Function]]

مراجعة 11:34، 30 أغسطس 2018

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

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

إن لم يمرَّر أي وسيط إلى الدالة sort()‎، فسترتِّب جميع عناصر المصفوفة أو القائمة المتغيرة التي استدعيت معها:

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)

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

أمَّا إن استدعيت الدالة sort()‎ مع تمرير وسيطين عددين، فسترتِّب المجال الذي يحدده هذين الوسيطين داخل المصفوفة التي استدعيت معها (بيئة التشغيل: JVM):

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)

إن استدعيت الدالة sort()‎ مع تمرير مًقارن 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 لترتيب عناصر مصفوفة:

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]
}

انظر أيضًا

مصادر