Sort better

This commit is contained in:
Jake Howard 2016-05-07 15:28:32 +01:00
parent a4e0b2b69d
commit d259c45b50
2 changed files with 20 additions and 9 deletions

View file

@ -3,6 +3,8 @@ import GiftedListView from 'react-native-gifted-listview';
import ProjectItem from './ProjectItem';
import { getProjects, getUserDetails } from '../../api/CircleCI';
import loaderHandler from 'react-native-busy-indicator/LoaderHandler';
import _ from 'underscore';
import sortObject from '../../helpers/sort-object';
const {
StyleSheet,
@ -30,21 +32,20 @@ export default class ProjectList extends React.Component {
this._fetch = this._fetch.bind(this);
}
componentDidMount() {
componentWillMount() {
getUserDetails().then(function(userDetails) {
this.setState({ userDetails });
}.bind(this));
}
_sortByDate(projects) {
return projects.sort(function (a, b) {
const keyA = a.username + '/' + a.reponame;
const keyB = b.reponame + '/' + b.username;
if (keyA < keyB) { return -1; }
if (keyA > keyB) { return 1; }
return 0;
}.bind(this));
return _.sortBy(projects, function (project) {
let mostRecentBranch = sortObject(project.branches, function (branch) {
return branch.recent_builds[0].pushed_at;
});
mostRecentBranch = mostRecentBranch[Object.keys(mostRecentBranch)[0]];
return mostRecentBranch.recent_builds[0].pushed_at;
}).reverse();
}
renderRow(project) {

View file

@ -0,0 +1,10 @@
import _ from 'underscore';
export default function sortObject(obj, comparator) {
let keys = _.sortBy(_.keys(obj), function (key) {
return comparator ? comparator(obj[key], key) : key;
});
return _.object(keys, _.map(keys, function (key) {
return obj[key];
}));
}