{"id":171,"date":"2020-10-12T12:17:57","date_gmt":"2020-10-12T12:17:57","guid":{"rendered":"https:\/\/barahasoft.com.np\/blog\/?post_type=codings&#038;p=171"},"modified":"2020-10-12T12:17:57","modified_gmt":"2020-10-12T12:17:57","slug":"kotlin-code-to-sort-list-of-objects-with-propertyattribute","status":"publish","type":"codings","link":"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/","title":{"rendered":"Kotlin code to sort list of objects with property(attribute)"},"content":{"rendered":"\n<p>Kotlin code to sort list is just a single line code. In this example we sort list of news in descending order on the basis of unixtime attribute of news.<\/p>\n\n\n\n<p><strong><em>News.kt<\/em><\/strong> <\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nimport androidx.room.Entity\nimport androidx.room.PrimaryKey\n\n@Entity(tableName = &quot;news&quot;)\ndata class News (\n        @PrimaryKey\n        var id: Long? = 0,\n        var title: String? = null,\n        var feature_image: String? = null,\n        var detail: String? = null,\n        var source: String? = null,\n        var link: String? = null,\n        var unixtime: Long = 0,\n        )\n\n<\/pre>\n\n\n<p><strong>code to sort news list:<\/strong><\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ creat array list\nvar newsList = ArrayList&lt;News&gt;()\n\n\/\/ create news and add to above newsList\nvar news1 = News()\nnews1.id = 1\nnews1.title = &quot;title1&quot;\nnews2.unixtime = 1000\nnewsList.add(news1)\n\nvar news2 = News()\nnews2.id = 2\nnews2.title = &quot;title2&quot;\nnews2.unixtime = 2000\nnewsList.add(news2)\n\nvar news3 = News()\nnews3.id = 3\nnews3.title = &quot;title3&quot;\nnews3.unixtime = 3000\nnewsList.add(news3)\n\nvar sortedNewsList = newsList.sortedByDescending { it.unixtime }\n\n                 \n<\/pre>","protected":false},"featured_media":0,"parent":0,"template":"","tags":[52,58,59],"language":[36],"ctype":[40],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Kotlin code to sort list of objects with property(attribute) - BS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin code to sort list of objects with property(attribute) - BS Blog\" \/>\n<meta property=\"og:description\" content=\"Kotlin code to sort list is just a single line code. In this example we sort list of news in descending order on the basis of unixtime attribute of news. News.kt code to sort news list:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/\" \/>\n<meta property=\"og:site_name\" content=\"BS Blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/\",\"name\":\"BS Blog\",\"description\":\"Baraha Soft Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/barahasoft.com.np\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/#webpage\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/\",\"name\":\"Kotlin code to sort list of objects with property(attribute) - BS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\"},\"datePublished\":\"2020-10-12T12:17:57+00:00\",\"dateModified\":\"2020-10-12T12:17:57+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/barahasoft.com.np\/blog\/codings\/kotlin-code-to-sort-list-of-objects-with-propertyattribute\/\"]}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings\/171"}],"collection":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings"}],"about":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/types\/codings"}],"wp:attachment":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/tags?post=171"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/language?post=171"},{"taxonomy":"ctype","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/ctype?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}