Here is a solution that works for most arrays of objects...
With very little changes, you could adapt this.