I've a column/field called last_message_time of type Date in my Table A.
Suppose querying Table A returns x results.
How do i sort these results based on dates inside last_message_time column.
Example, in SQLite we have ORDER BY date(dateColumn)
RealmResults<A> sorted = realm.where(A.class)
.findAllSorted("last_message_time", Sort.ASCENDING);
EDIT: since Realm 4.3.0, the following is preferred:
RealmResults<A> sorted = realm.where(A.class)
.sort("last_message_time", Sort.ASCENDING)
.findAll();
Use just "sort"!
"findAllSorted" is deprecated!
io.realm.RealmQuery.findAllSorted(String)
Since 4.3.0, now use RealmQuery.sort(String) then RealmQuery.findAll() Finds all objects that fulfill the query conditions and sorted by specific field name in ascending order.
Sorting is currently limited to character sets in 'Latin Basic', 'Latin Supplement', 'Latin Extended A', 'Latin Extended B' (UTF-8 range 0-591). For other character sets, sorting will have no effect.
More details in: LINK
Related
Making a query to realtime firebase
DatabaseReference mReference = mDatabase.getReference("Cards/" + language + "/" + gameTitle);
Query query = mReference.orderByChild("rank");
query.addValueEventListener
it suppose to sort it by rank and give as 1/2/3/4/5/6 values. Instead of that it returns 1/10/11/12/13/14/15/16/17/18/19/2/20/21 etc...
Thats how hierarchy looks like (sorry for russian language but dont have english yet)
Looks like it sorting by first character instead of whole value
it suppose to sort it by rank and give as 1/2/3/4/5/6 values. Instead of that it returns 1/10/11/12/13/14/15/16/17/18/19/2/20/21
That's the normal behaviour since you are ordering the results according to the rank property, which is of type String and not number. When you order strings, the elements are ordered lexicographically.
There are two ways in which you can solve this, either use my solution from the above answer or you change the type of your property to number. In my opinion, since all those value are numbers, is the solution you should go ahead with.
How can I change the order or index of Realm object in the android studio??
I want to change the order of the objects, I cleared the class and tried to insert a new one, Is that the solution. Please help me
Instead of using findAll() method you can use findAllSorted() and you can also give ASCENDING or DESCENDING order.
From Realm documentation:
findAllSorted(java.lang.String[] fieldNames, Sort[] sortOrders)
Finds all objects that fulfill the query conditions and sorted by
specific field names.
Parameters:
fieldNames - an array of field names to sort by.
sortOrders - how to sort the field names.
Returns: a RealmResults containing objects. If no objects match the
condition, a list with zero objects is returned.
I just inserted a column sort_order and updated that each time.
Works perfectly. Thanks
I have the JSON structure above
I want to get the list of vehicles where their ids ends with "123"
I had tried to use Query.endAt() method but i'm not sure if i'm using it right or it shouldn't give the required output
Query vehiclesRef;
vehiclesRef = db.getReference("vehicles").orderByKey().endAt("\uf8ff123");
Firebase Database queries can only perform prefix matches, so strings starting with a specific string or starting with a range of string. It is not possible to query for strings ending with a specific value, nor those ending with a range of values.
If you really want to do this within Firebase, consider also storing the reversed strings in the database:
"321_1_0"
"321_3_0"
"654_52_0"
Then you can query for strings starting with 321 with
vehiclesQuery = db.getReference("vehicles").orderByKey().startAt("321").endAt("321\uf8ff");
I have a list of names of starts with characters and end with numbers like: -
ka1, ka10, ka 2, ka, sa2, sa1, sa10, p1a10, 1kb, p1a2, p1a11, p1a.
I want to sort it in natural order, that is: -
1kb, ka, ka1, ka 2, ka10, p1a, p1a2, p1a10, p1a11, sa1, sa2, sa10.
The main problem I am seeing here is no delimiter between text and numeric part, there also a chance of without numeric part also.
I am using sqlite in android, I can do sorting using java after fetching points by cacheing cursor data, but I am using(recommended to use) cursor adapter.
Please suggest a query for sorting or is there any way to apply sorting in cursor?
I tried below query for Natural sorting:
SELECT
item_no
FROM
items
ORDER BY
LENGTH(item_no), item_no;
It worked for me in Sqlite db too. Please see this link, for more details.
I can propose using regex replacement adding zeros, creating temporary table of original and corresponding values, then follow this link for sorting it: http://www.saltycrane.com/blog/2007/12/how-to-sort-table-by-columns-in-python/
tip for regex add as many zeros after last letter, but limit the number of total digits for predicted maximum number of digits. If You need help with regex as well, provide exact info of valid and invalid values, so can halp with that too.
PS if want to be sure that zeros goes before last digits search for char from the end
Updated
You can use different ways - Some of are mentioned below:
BIN Way
SELECT
tbl_column,
BIN(tbl_column) AS binray_not_needed_column
FROM db_table
ORDER BY binray_not_needed_column ASC , tbl_column ASC
Cast Way
SELECT
tbl_column,
CAST(tbl_column as SIGNED) AS casted_column
FROM db_table
ORDER BY casted_column ASC , tbl_column ASC
or try the solution:
There are a whole lot of solutions out there if you hit up Google, and
you can, of course, just use the natsort() function in PHP, but it's
simple enough to accomplish natural sorting in MySQL: sort by length
first, then the column value.
Query: SELECT alphanumeric, integer FROM sorting_test ORDER BY LENGTH(alphanumeric), alphanumeric from here
How I can select first N element from Realm database.
Now,I use this code to select N elements:
Realm.getDefaultInstance()
.where(MyObject.class)
.findAllSorted("myField", Sort.DESCENDING)
But this select operation is too long. I need to SQL 'LIMIT' operation analog.
You can have a look at this
https://github.com/realm/realm-java/issues/544
seems realm results are lazy loaded so you dun need "limit" when using realm
Simple
int N=10; // whatever value you want
Realm mRealm=Realm.getDefaultInstance();
RealmResults<Example> list= mRealm.where(Example.class).findAll();
list.subList(0,N);
I did that using limit method, You should use latest version classpath "io.realm:realm-gradle-plugin:5.8.0"
RealmResults<YourPOJOClass> realmResults = mRealm.where(YourPOJOClass.class).sort("createdTime").limit(10).findAll();
//here this record will be sorted by ascending order using schema name "createdTime"
//this will return the 10 rows only.
`
Possible duplicate Since I already posted the solution here : Limit Realm results
I figured out the solution to achieve this after so many days using "between" query as found in the official docs https://realm.io/docs/java/latest
If you want to fetch first N elements, Simply pass the count from and to with field name as below
realm.where(clazz).between("count",0,1000).findAll();
Where,
"count" = Field name (i.e variable name in your pojo)
"0" = Range From
"1000" = Range to
For example: The above query will fetch first 0 to 1000 as RealmResults.
Note: The above solution works only if you have some unique id with row count. In my case I manually inserted count value before inserting the values into Realm.