How can I sort a table based two fields using Anko in Kotlin?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I use Anko to operate SQLite table, I hope to sort a list based the field isFavorite
first, then based the createdDate
For example, input Source Data, and I get the Sort Result Data
But the code return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
can't get the correct result, how can I fix it?
Source Data
Sort Result Data (I hope to get )
Structure
data class MRecord (
var _id: Long,
var isFavorite: Bool,
var createdDate: Long
)
Code
var myList=select(tableName).parseList { MDBRecord(HashMap(it)) }
return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
Added Content
To madhead: Thanks!
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
I hope to get the result
1, 1, 100
3, 1, 300
2, 0, 200
but the code println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
will not work because isFavorite is Long, how can I fix it?
kotlin anko
add a comment |
I use Anko to operate SQLite table, I hope to sort a list based the field isFavorite
first, then based the createdDate
For example, input Source Data, and I get the Sort Result Data
But the code return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
can't get the correct result, how can I fix it?
Source Data
Sort Result Data (I hope to get )
Structure
data class MRecord (
var _id: Long,
var isFavorite: Bool,
var createdDate: Long
)
Code
var myList=select(tableName).parseList { MDBRecord(HashMap(it)) }
return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
Added Content
To madhead: Thanks!
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
I hope to get the result
1, 1, 100
3, 1, 300
2, 0, 200
but the code println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
will not work because isFavorite is Long, how can I fix it?
kotlin anko
add a comment |
I use Anko to operate SQLite table, I hope to sort a list based the field isFavorite
first, then based the createdDate
For example, input Source Data, and I get the Sort Result Data
But the code return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
can't get the correct result, how can I fix it?
Source Data
Sort Result Data (I hope to get )
Structure
data class MRecord (
var _id: Long,
var isFavorite: Bool,
var createdDate: Long
)
Code
var myList=select(tableName).parseList { MDBRecord(HashMap(it)) }
return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
Added Content
To madhead: Thanks!
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
I hope to get the result
1, 1, 100
3, 1, 300
2, 0, 200
but the code println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
will not work because isFavorite is Long, how can I fix it?
kotlin anko
I use Anko to operate SQLite table, I hope to sort a list based the field isFavorite
first, then based the createdDate
For example, input Source Data, and I get the Sort Result Data
But the code return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
can't get the correct result, how can I fix it?
Source Data
Sort Result Data (I hope to get )
Structure
data class MRecord (
var _id: Long,
var isFavorite: Bool,
var createdDate: Long
)
Code
var myList=select(tableName).parseList { MDBRecord(HashMap(it)) }
return myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
Added Content
To madhead: Thanks!
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
I hope to get the result
1, 1, 100
3, 1, 300
2, 0, 200
but the code println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
will not work because isFavorite is Long, how can I fix it?
kotlin anko
kotlin anko
edited Nov 17 '18 at 12:57
HelloCW
asked Nov 16 '18 at 8:36
HelloCWHelloCW
162432103
162432103
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You're right, myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
will not give you correct results, because it is basically equal to myList.sortedBy{ it.createdDate}
(only the last sorting applies). What you need is a comparator that takes multiple fields into account and looks like compareBy
is what you need:
Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.
So, check this out:
data class MRecord(
var id: Long,
var isFavorite: Boolean,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, true, 10),
MRecord(2, false, 20),
MRecord(3, true, 30)
)
println(list)
println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
}
Note how you can mix lambdas and function references.
Edit for Long
isFavorite:
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
println(list.sortedWith(compareByDescending<MRecord> { it.isFavorite }.then(compareBy(MRecord::createdDate))))
}
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTWlist.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work
– HelloCW
Nov 17 '18 at 12:14
1
Trylist.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334120%2fhow-can-i-sort-a-table-based-two-fields-using-anko-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're right, myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
will not give you correct results, because it is basically equal to myList.sortedBy{ it.createdDate}
(only the last sorting applies). What you need is a comparator that takes multiple fields into account and looks like compareBy
is what you need:
Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.
So, check this out:
data class MRecord(
var id: Long,
var isFavorite: Boolean,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, true, 10),
MRecord(2, false, 20),
MRecord(3, true, 30)
)
println(list)
println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
}
Note how you can mix lambdas and function references.
Edit for Long
isFavorite:
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
println(list.sortedWith(compareByDescending<MRecord> { it.isFavorite }.then(compareBy(MRecord::createdDate))))
}
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTWlist.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work
– HelloCW
Nov 17 '18 at 12:14
1
Trylist.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
add a comment |
You're right, myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
will not give you correct results, because it is basically equal to myList.sortedBy{ it.createdDate}
(only the last sorting applies). What you need is a comparator that takes multiple fields into account and looks like compareBy
is what you need:
Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.
So, check this out:
data class MRecord(
var id: Long,
var isFavorite: Boolean,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, true, 10),
MRecord(2, false, 20),
MRecord(3, true, 30)
)
println(list)
println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
}
Note how you can mix lambdas and function references.
Edit for Long
isFavorite:
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
println(list.sortedWith(compareByDescending<MRecord> { it.isFavorite }.then(compareBy(MRecord::createdDate))))
}
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTWlist.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work
– HelloCW
Nov 17 '18 at 12:14
1
Trylist.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
add a comment |
You're right, myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
will not give you correct results, because it is basically equal to myList.sortedBy{ it.createdDate}
(only the last sorting applies). What you need is a comparator that takes multiple fields into account and looks like compareBy
is what you need:
Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.
So, check this out:
data class MRecord(
var id: Long,
var isFavorite: Boolean,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, true, 10),
MRecord(2, false, 20),
MRecord(3, true, 30)
)
println(list)
println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
}
Note how you can mix lambdas and function references.
Edit for Long
isFavorite:
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
println(list.sortedWith(compareByDescending<MRecord> { it.isFavorite }.then(compareBy(MRecord::createdDate))))
}
You're right, myList.sortedBy{it.isFavorite}.sortedBy{ it.createdDate}
will not give you correct results, because it is basically equal to myList.sortedBy{ it.createdDate}
(only the last sorting applies). What you need is a comparator that takes multiple fields into account and looks like compareBy
is what you need:
Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.
So, check this out:
data class MRecord(
var id: Long,
var isFavorite: Boolean,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, true, 10),
MRecord(2, false, 20),
MRecord(3, true, 30)
)
println(list)
println(list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate)))
}
Note how you can mix lambdas and function references.
Edit for Long
isFavorite:
data class MRecord(
var id: Long,
var isFavorite: Long,
var createdDate: Long
)
fun main(args: Array<String>) {
val list = listOf(
MRecord(1, 1, 100),
MRecord(2, 0, 200),
MRecord(3, 1, 300)
)
println(list.sortedWith(compareByDescending<MRecord> { it.isFavorite }.then(compareBy(MRecord::createdDate))))
}
edited Nov 17 '18 at 18:21
answered Nov 16 '18 at 15:07
madheadmadhead
15k1389127
15k1389127
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTWlist.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work
– HelloCW
Nov 17 '18 at 12:14
1
Trylist.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
add a comment |
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTWlist.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work
– HelloCW
Nov 17 '18 at 12:14
1
Trylist.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTW
list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work– HelloCW
Nov 17 '18 at 12:14
Thanks! Your code works well, but how can I do if I hope to get a descending result of createdDate ? BTW
list.sortedWith(compareBy({ !it.isFavorite }, MRecord::createdDate.reverse))
isn't work– HelloCW
Nov 17 '18 at 12:14
1
1
Try
list.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Try
list.sortedWith(compareBy<MRecord>{ !it.isFavorite }.thenDescending(compareBy(MRecord::createdDate)))
– madhead
Nov 17 '18 at 12:26
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
Thanks! Would you please to see my added content in the question?
– HelloCW
Nov 17 '18 at 12:57
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
@HelloCW, see my edit
– madhead
Nov 17 '18 at 18:21
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
Thank you very much, your code is great!
– HelloCW
Nov 18 '18 at 1:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334120%2fhow-can-i-sort-a-table-based-two-fields-using-anko-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown