Sorting a list of objects based on their coordinate( lat and long) using Haversine formula

Hi Kotlin Developers,

I’m new to kotlin and want to sort a list based on their latitude and longitude…
Here is my code:

import java.util.*
import com.google.gson.GsonBuilder
import java.io.File
import java.io.InputStream
import java.util.Comparator

data class Property(val Pcode: Int, val Locality: String, val State: String, val Comments: String, val Category: String, val Longitude: Double, val Latitude: Double)


class SortPlaces(currentLatitude: Double, currentLongitude: Double) : Comparator<Property> {
    var currentLat: Double
    var currentLng: Double
    override fun compare(property1: Property, property2: Property): Int {
        val lat1: Double = property1.Latitude
        val lon1: Double = property1.Longitude
        val lat2: Double = property2.Latitude
        val lon2: Double = property2.Longitude
        val distanceToPlace1 = distance(currentLat, currentLng, lat1, lon1)
        val distanceToPlace2 = distance(currentLat, currentLng, lat2, lon2)
        return (distanceToPlace1 - distanceToPlace2).toInt()
    }

    fun distance(fromLat: Double, fromLon: Double, toLat: Double, toLon: Double): Double {
        val radius = 6378137.0 // approximate Earth radius, *in meters*
        val deltaLat = toLat - fromLat
        val deltaLon = toLon - fromLon
        val angle = 2 * Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(deltaLat / 2), 2.0) +
                        Math.cos(fromLat) * Math.cos(toLat) *
                        Math.pow(Math.sin(deltaLon / 2), 2.0)
            )
        )
        return radius * angle
    }

    init {
        currentLat = currentLatitude
        currentLng = currentLongitude
    }
}

fun main(args: Array<String>) {
    val command = Scanner(System.`in`)
    val running = true

    while (running) {

        val inputStream: InputStream = File("./src/main/kotlin/suburbs.json").inputStream()
        val inputString = inputStream.bufferedReader().use { it.readText() }
        val gson = GsonBuilder().create()
        val packagesArray = gson.fromJson(inputString , Array<Property>::class.java).toList()

        println("Please enter a suburb name: ")
        val suburbName = command.nextLine()

        println("Please enter the postcode: ")
        val postcode = command.nextLine()

        val userProperty: Property? = packagesArray.find{ it.Locality.toLowerCase().equals(suburbName.toLowerCase()) && it.Pcode == postcode.toInt()}

        //sort the list, give the Comparator the current location
        Collections.sort(packagesArray, new SortPlaces(userProperty.Latitude, userProperty.Longitude));

    }
    command.close()
}

I got error: Too many arguments for public open fun <T : Comparable<T!>!> sort(list: (Mutable)List<T!>!): Unit defined in java.util.Collections
at my sort{} function
my userProperty has to be Property? because the find{} method return Property?
Then Collections.sort() can not sort Property? type because the SortPLaces only accept Comparator not Comparator<Property?>
What should I do ? Thank you guys for reading this post.

You are basically mixing Java code and Kotlin code. The error message is misleading in your case, the problem is with new operator which doesn’t exist in Kotlin. Just remove it and leave SortPlaces(...) only.

Collections.sort() isn’t really how we sort in Kotlin - collections have functions/extensions to perform sorting.