Sort Array of AWS3 objects - ruby-on-rails

I have a collection of aws objects. I would like to sort the objects according to last modified time. See the below snippet
array = [<AWS::S3::S3Object:dt_publisher_reports/temp/2013.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_October.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_September_176.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_September_1764.csv>
]
I need to sort the array with respect to last modified time of that particular file.

Try this
array = [<AWS::S3::S3Object:dt_publisher_reports/temp/2013.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_October.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_September_176.csv>,
<AWS::S3::S3Object:dt_publisher_reports/temp/2013_September_1764.csv>]
array.sort_by &:last_modified

Use this
array.sort_by &:updated_at
or
array.order(:updated_at)

Related

programatically making a 2d array in swift

I need to use a for loop to create a 2d array. So far "+=" and .append have not yielded any results. Here is my code. Please excuse the rushed variable naming.
let firstThing = contentsOfFile!.componentsSeparatedByString("\n")
var secondThing: [AnyObject] = []
for i in firstThing {
let temp = i.componentsSeparatedByString("\"")
secondThing.append(temp)
}
The idea is that it takes the contents of a csv file, then separates the individual lines. It then tries to separate each of the lines by quotation marks. This is where the problem arises. I am successfully making the quotation separated array (stored in temp), however, I cannot make a collection of these in one array (i.e. a 2d array) using the for loop. The above code generates an error. Does anybody have the answer of how to construct this 2d array?
You can do this using higher order functions...
let twoDimensionalArray = contentsOfFile!.componentsSeparatedByString("\n").map{
$0.componentsSeparatedByString("\"")
}
The map function takes an array of items and maps each one to another type. In this I'm mapping the strings from the first array into an array pf strings and so creating a 2d array.
This will infer the type of array that is created so no need to put [[String]].
Here you go...

Summing 5 objects after a where statement

I have an active record of 5 objects as follows
#top_sold = Photo.where(photographer_id: #photog).order('qty_sold DESC').first(5)
I want to know the sum of qty_sold for all 5 photos
This isnt working
#top_sold.sum(:qty_sold)
Much thanks in adcance
.first returns Array, use limit instead (which returns ActiveRecord::Relation).
#top_sold = Photo.where(photographer_id: #photog).order('qty_sold DESC').limit(5)
for Array you can do
#top_sold.map(:qty_sold).inject(:+)
Have you tried appending sum to your query?
#top_sold = Photo.where(photographer_id: #photog)
.order('qty_sold DESC').first(5).sum('qty_old')
If you need the original result set, then #Nithin answer is what you want to do.

Get specific column from a collection ActiveRecord objects?

How can I extract a list of IDs from a returned ActiveRecord results without re-querying the ID column alone?
For exmaple:
people = People.all
people.get_ids #Returns an array of IDS
My current solution is to loop through people and get the ID manually(which isn't very elegant, IMHO)
You can use pluck method:
e.g. People.pluck(:id)
Refer http://apidock.com/rails/ActiveRecord/Calculations/pluck
Fetching using pluck would be better compared to the process of fetching the same after taking all results.
Hope that helps..
You can use map method
all_ids = people.map(&:id) #[1,2,3,4]
ids = People.all.map(&:id)
This code will return array of ids

Sort multiple arrays

Here is my situation:
I manipulate 6 NSMutableArrays. One of them has NSDates objects in it, the other ones have NSNumbers. When I populate them, I use addObject: for each of them, so index 0 of each array contains all the values I want for my date at index 0 in the dates array.
I want to make sure that the arrays are all sorted according to the dates array (order by date, ascending), meaning that during the sorting, if row 5 of the dates array is moved to row 1, it has to be applied to all the other arrays as well.
I was previously using CoreData, but I must not use it anymore (please don't ask why, this is off-topic ;) ). In CoreData, I could use an NSSortDescriptor, but I have no idea on how to do it with multiple NSArrays...
As always, hints/answers/solutions are always appreciated :)
This is a common problem. Use the following approach.
Encapsulate your six arrays into an object - every instance will have six properties.
Implement compare: method on this object, using [NSDate compare:] (this step can be skipped but it's cleaner this way).
Now you have only one array - sort it using the method from step 2.
I think the better solution for you to have NSArray of NSDictionary objects.
NSArray *allValues = [[NSArray alloc] init];
NSDictionary *dict = #{"Date" : [NSDate date], #"Key1" : #"Value1", #"Key2" : #"Value2"};
Then you can sort this array with sortDescriptor without any problems.
And then you can also use Comparator or Sort Desriptor as you wish.
Wrap all your items that you are storing in an array into a single object. Each one of your previous 6 arrays will be a property.
Inside that object you can implement
- (NSComparisonResult)compare:(YourClass *)otherObject {
return [self.date compare:otherObject.date];
}
You can now sort the array and they will sort by date.

How to create object array in rails?

I need to know how to create object array in rails and how to add elements in to that.
I'm new to ruby on rails and this could be some sort of silly question but I can't find exact answer for that. So can please give some expert ideas about this
All you need is an array:
objArray = []
# or, if you want to be verbose
objArray = Array.new
To push, push or use <<:
objArray.push 17
>>> [17]
objArray << 4
>>> [17, 4]
You can use any object you like, it doesn't have to be of a particular type.
Since everything is an object in Ruby (including numbers and strings) any array you create is an object array that has no limits on the types of objects it can hold. There are no arrays of integers, or arrays of widgets in Ruby. Arrays are just arrays.
my_array = [24, :a_symbol, 'a string', Object.new, [1,2,3]]
As you can see, an array can contain anything, even another array.
Depending on the situation, I like this construct to initialize an array.
# Create a new array of 10 new objects
Array.new(10) { Object.new }
#=> [#<Object:0x007fd2709e9310>, #<Object:0x007fd2709e92e8>, #<Object:0x007fd2709e92c0>, #<Object:0x007fd2709e9298>, #<Object:0x007fd2709e9270>, #<Object:0x007fd2709e9248>, #<Object:0x007fd2709e9220>, #<Object:0x007fd2709e91f8>, #<Object:0x007fd2709e91d0>, #<Object:0x007fd2709e91a8>]
Also if you need to create array of words next construction could be used to avoid usage of quotes:
array = %w[first second third]
or
array = %w(first second third)

Resources