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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 3: سطر 3:
  
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة  <code>slice()‎</code> عبر  [[Kotlin/Array|مصفوفة]] فستقوم بترتيبها: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] فستقوم بترتيبها: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
 
fun IntArray.sort()  
 
fun IntArray.sort()  
 
fun LongArray.sort()  
 
fun LongArray.sort()  
سطر 13: سطر 13:
 
inline fun <T : Comparable<T>> Array<out T>.sort() (source)
 
inline fun <T : Comparable<T>> Array<out T>.sort() (source)
  
</syntaxhighlight>في حال استدعاء الدالة  <code>slice()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل [[Kotlin/Array|المصفوفة]]: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل [[Kotlin/Array|المصفوفة]]: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
 
fun <T> Array<out T>.sort(
 
fun <T> Array<out T>.sort(
 
     fromIndex: Int = 0,  
 
     fromIndex: Int = 0,  
سطر 27: سطر 27:
 
</syntaxhighlight>'''بيئة التشغيل: JVM'''
 
</syntaxhighlight>'''بيئة التشغيل: JVM'''
  
في حال استدعاء الدالة  <code>slice()‎</code> عبر  كائن من النوع <code>[[kotlin/MutableList|MutableList]]</code> فستقوم بترتيبه:<syntaxhighlight lang="kotlin">
+
في حال استدعاء الدالة  <code>sort()‎</code> عبر  كائن من النوع <code>[[kotlin/MutableList|MutableList]]</code> فستقوم بترتيبه:<syntaxhighlight lang="kotlin">
 
fun <T : Comparable<T>> MutableList<T>.sort()  
 
fun <T : Comparable<T>> MutableList<T>.sort()  
  
</syntaxhighlight>في حال استدعاء الدالة  <code>slice()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير مًقارن <code>comparison</code> كمعامل فستقوم بترتيب تلك [[Kotlin/Array|المصفوفة]] بحسب الترتيب الذي يحدده المقارن <code>comparison</code>: <syntaxhighlight lang="kotlin">
+
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير مًقارن <code>comparison</code> كمعامل فستقوم بترتيب تلك [[Kotlin/Array|المصفوفة]] بحسب الترتيب الذي يحدده المقارن <code>comparison</code>: <syntaxhighlight lang="kotlin">
 
inline fun <T> Array<out T>.sort(
 
inline fun <T> Array<out T>.sort(
 
     noinline comparison: (a: T, b: T) -> Int)
 
     noinline comparison: (a: T, b: T) -> Int)

مراجعة 18:10، 6 يونيو 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)

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

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 في حال استدعاء الدالة  sort()‎ عبر كائن من النوع MutableList فستقوم بترتيبه:

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

في حال استدعاء الدالة  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 مع المصفوفات

تعرف الشيفرة الآتية  مصفوفةباسم 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]
}

أنظر أيضًا

مصادر