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