What is the best and most efficient method of fetching the maximum value from an Int column?

What is the best and most efficient method of fetching the maximum value from an Int column?

Idea A

let maxId = realm.objects(Books).sorted("id").last

Idea B

let maxId = realm.objects(Books).sorted("id", ascending: false).first

Or another idea?

(Yes my code snippets will only return the object with the highest ID, not the actual value)

I believe it should work in the following way

 if let maxValue =  realm.objects(Books).max("id") as Int?{

 // Do some stuff 
 }

Or just

let maxValue =  realm.objects(Books).max("id") as Int?

To make my answer complete I decided to add the code to fetch the min value: realm.objects(Books).min("id") as Int?

Find MAX value from multiple columns in a SQL Server table, In this tip, we will look a different ways that this can be done. Find MAX value from multiple columns in a SQL Server table CREATE TABLE ##TestTable ( ID INT IDENTITY(1,1) PRIMARY KEY, Name So as we can see, the first query is the most optimal also it has compact code and is a good choice  Within this view, it adds an extra column which is the maximum start_date value which is partitioned by city. This field has the same value for each city, which is the maximum. Then, outside this view, in the WHERE clause, it only shows those where the start_date value is the maximum.

Following on from @ProblemSlover's answer. I created a small app that throws 10000 records into a realm class - calling a function to fetch the max value and using that to set the ID column. I wanted to see some metrics (I ran the test 3 times to get an average). As you can see the MAX function is 2.3 times quicker than sorted/last and nearly 8 times quicker than ascending/first. Sometimes it's just good to know. :)

k largest(or smallest) elements in an array, Question: Write an efficient program for printing k largest elements in an array. 1) Modify Bubble Sort to run the outer loop at most k times. int arr[] = { 1, 23, 12, 9, 30, 2, 50 };. int n = sizeof (arr) / sizeof (arr[0]);. int k = 3; find any of the above explanations/algorithms incorrect, or find better ways to solve the same problem. What is the best and most efficient method of fetching the maximum value from an Int column? Idea A. let maxId = realm.objects(Books).sorted("id").last Idea B. let maxId = realm.objects(Books).sorted("id", ascending: false).first Or another idea? (Yes my code snippets will only return the object with the highest ID, not the actual value)

Swift 4 and Xcode 9.3

I tried ProblemSlover's answer - but it doesn't work anymore...

But give this a try:

let maxValue =  realm.objects(Books).max(ofProperty: "id") as Int?

Effective SQL: 61 Specific Ways to Write Better SQL, 61 Specific Ways to Write Better SQL John L. Viescas, Douglas J. Steele, Ben G. a table with a single column of sequential numbers, with values starting from 1 (or 0) to a maximum number appropriate for the situation. It can also be a single column of sequential dates that cover a range of interest, or something more  In SQL Server we can find the maximum or minimum value from different columns of the same data type using different methods. Performance and compact code are essential. As we can see the first solution in our article is the best in performance and it also has relatively compact code.

Program to find largest element in an array, The solution is to initialize max as first element, then traverse the given array from second 324 , 45 , 90 , 9808 };. // Method to find maximum in arr[]. static int largest(). {. int i; or you want to share more information about the topic discussed above Find the original matrix when largest element in a row and a column are  MAX sql command will return the record with maximum or highest value in the SQL table. Same way we can get the minimum value of a range of records by using SQL MIN command What happen if we apply MAX command to a non numeric field? We will get the record with highest alphabet. But the best way to use MAX command is to apply it in a numeric field.

MySQL 5.0 Certification Study Guide, When followed by a single integer, row_count, LIMIT returns the first When you need only some of the rows selected by a query, LIMIT is an efficient way to obtain them. For a client application that fetches rows from the server, you get better is to find the row containing the smallest or largest values in a particular column. It's the simplest and most efficient way to fetch multiple rows from a dynamically-constructed SELECT. One caution: this sort of un-limited BULK COLLECT can result in the consumption of very large amounts of Process Global Area memory.

SQLite MAX: Get the Maximum Value in a Set, For example, you can use the MAX function to find the most expensive products, The expression can be a column of a table or an expression that consists of integer, real, text, blob, and NULL in SQLite, when comparing values to find the It explains the complex concepts in simple and easy-to-understand ways so that  This gives the list of all the column names and its maximum value, so the output will be . Get the maximum value of a specific column in pandas: Example 1: # get the maximum value of the column 'Age' df['Age'].max() This gives the maximum value of column “Age” so the output will be

Comments
  • Just looked into source code of realm and there is presented a method max<U: MinMaxType>(property: String) . The end result it should look like below Realm().objects(Books).max("id") OR realm.objects(Books).max("id") Here is the link to the source github.com/realm/realm-cocoa/blob/master/RealmSwift-swift2.0/… Please Correct me If I'm wrong :)
  • :( All I'm getting is "generic parameter 'U' could not be inferred". The docs sure point to what you suggest being possible. I've tried all manner of combinations for using 'max' that I can think of but no success.
  • Sorry if my suggestion sounds naive, but Have you tried to make it working this way? realm.objects(Books).max("id") as Int?
  • Whoever Down-voted my answer should leave the comment why!