/* Program to read in tab-separated values with information about
 * congressional members, sort that data, and print out the first
 * 10 results.
 * Right now it just sorts based on first names and prints out the
 * first names.
 * Your challenge: Use a struct to store more information about
 * each congressperson. Have your program sort them according to age
 * and then print out the first and last names of the 10 youngest
 * congressional members.
 */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool readcong(string& rep, istream& fin);
void sort(string* arr, int n);
bool before(string a, string b);

int main() {
  ifstream congfile("legislators-current.tsv");

  string* congress = new string[1000];
  int n=0;

  while (readcong(congress[n], congfile)) {
    ++n;
  }

  sort(congress, n);

  for (int i=0; i<10; ++i) {
    cout << congress[i] << endl;
  }

  delete [] congress;
  return 0;
}

bool readcong(string& rep, istream& fin) {
  int birthyear, birthmonth, birthday;
  string last_name, first_name, gender, type, state, district, party, 
         url, address, phone, contact_form, rss_url, twitter, facebook, 
         facebook_id, youtube, youtube_id, bioguide_id, thomas_id, 
         opensecrets_id, lis_id, cspan_id, govtrack_id, votesmart_id, 
         ballotpedia_id, washington_post_id, icpsr_id, wikipedia_id;
  fin >> last_name >> first_name >> birthyear >> birthmonth >> birthday 
      >> gender >> type >> state >> district >> party >> url >> address >> phone 
      >> contact_form >> rss_url >> twitter >> facebook >> facebook_id 
      >> youtube >> youtube_id >> bioguide_id >> thomas_id >> opensecrets_id 
      >> lis_id >> cspan_id >> govtrack_id >> votesmart_id >> ballotpedia_id 
      >> washington_post_id >> icpsr_id >> wikipedia_id;
  rep = first_name;
  return bool(fin);
}

bool before(string a, string b) {
  return a < b;
}

void sort(string* arr, int n) {
  for (int i=n; i>1; --i) {
    int m=0;
    for (int j=1; j<i; ++j) {
      if (before(arr[m], arr[j])) m=j;
    }
    string temp = arr[m];
    arr[m] = arr[i-1];
    arr[i-1] = temp;
  }
}