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

**URL:** <https://discuss.kotlinlang.org/t/sorting-a-list-of-objects-based-on-their-coordinate-lat-and-long-using-haversine-formula/23876>\
**Category:** Support\
**Created:** [January 10, 2022, 9:48am UTC](https://discuss.kotlinlang.org/t/sorting-a-list-of-objects-based-on-their-coordinate-lat-and-long-using-haversine-formula/23876 "2022-01-10T09:48:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylelw23](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kylelw23](https://discuss.kotlinlang.org/u/kylelw23)\
**Post date:** [January 10, 2022, 9:48am UTC](https://discuss.kotlinlang.org/t/sorting-a-list-of-objects-based-on-their-coordinate-lat-and-long-using-haversine-formula/23876/1 "2022-01-10T09:48:59Z")

</div>

Hi Kotlin Developers,

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [January 10, 2022, 11:32am UTC](https://discuss.kotlinlang.org/t/sorting-a-list-of-objects-based-on-their-coordinate-lat-and-long-using-haversine-formula/23876/2 "2022-01-10T11:32:31Z")

</div>

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.
