Member Avatar for iamthwee

Ok basically, I'm trying to sort by class definition in c++.

For example I have a class:-

class crap
{
     private:
                string word;
                int word_length;
 ...

Which gets words from a list and then sorts them by word length or in alphabetical order. However, I couldn't find anything useful. :cry: :cry: :eek: :cool: :rolleyes:

I found something using structs in c however, I want to do this is c++.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NELEMS 4

struct crap
{
   char word[163];
   int word_length;
};

static int name_comp(const void *, const void *);
static int word_length_comp(const void *, const void *);

int main()
{
   size_t i;
   static struct crap stuff[NELEMS] =
     {{"yo",2},
      {"momma",5},
      {"iz",2},
      {"phat",4}};

   qsort(stuff, NELEMS, sizeof stuff[0],
        name_comp);

   puts("By alphabetical order:");
   for (i = 0; i < NELEMS; ++i)
      printf("%s, %d\n",
        stuff[i].word,
        stuff[i].word_length);

   qsort(stuff, NELEMS, sizeof stuff[0],
        word_length_comp);

   puts("\nBy word length:");
   for (i = 0; i < NELEMS; ++i)
      printf("%s, %d\n",
        stuff[i].word,
        stuff[i].word_length);
        
        getchar();
        getchar();
   return 0;
}

static int name_comp(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   int order = strcmp(sp1->word,sp2->word);

  
   return order;
}

static int word_length_comp(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;

   return sp1->word_length - sp2->word_length;
}

God bless.

Recommended Answers

All 9 Replies

Maybe I am not understanding your question, but can't you use a std::list for that?

How about a vector of strings and the std::sort?

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int bylen(const std::string &s, const std::string &t)
{
   return s.length() < t.length();
}

int main()
{
   static const char *init[] = {"yo","momma","iz","phat"};
   std::vector<std::string> word(init, init + sizeof init / sizeof *init);
   
   std::cout << "--unsorted--\n";
   copy(word.begin(), word.end(), 
        std::ostream_iterator<std::string>(std::cout, "\n"));
   
   std::cout << "--sorted by string--\n";
   sort(word.begin(), word.end());
   copy(word.begin(), word.end(), 
        std::ostream_iterator<std::string>(std::cout, "\n"));
   
   std::cout << "--sorted by length--\n";
   sort(word.begin(), word.end(), bylen);
   copy(word.begin(), word.end(), 
        std::ostream_iterator<std::string>(std::cout, "\n"));
   return 0;
}

/* my output
--unsorted--
yo
momma
iz
phat
--sorted by string--
iz
momma
phat
yo
--sorted by length--
iz
yo
phat
momma
*/
Member Avatar for iamthwee

No, those all work, but I'm after a way to do this using a class.

So I have a class:- crap


Then I can create an array of objects for that class.

Then I can sort that class by their class definition?


Is that making any sense?

Then I can sort that class by their class definition?
Is that making any sense?

I don't get the above part. You have only one class, no?

Member Avatar for iamthwee

I don't know how good your java is but essentially I want a layout like this:-

class Person
{
private String lastName;
private String firstName;
private int day;
private int month;
//----------------------------------------------------------
 
public Person(String last, String first, int d, int m)
{ // constructor
lastName = last;
firstName = first;
day = d;
month = m;
}
//----------------------------------------------------------
 
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Day: " + day);
System.out.println(", Month:" + month);
}
//----------------------------------------------------------
 
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
 
class ArrayInOb
{
private Person[] a; // ref to array a
private int nElems; // number of data items
 
//-------------------------------------------------------------
 
public ArrayInOb(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------
 
// put person into array
public void insert(String last, String first, int day, int month)
{
a[nElems] = new Person(last, first, day, month);
nElems++; // increment size
}
//-------------------------------------------------------------
 
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
System.out.println("");
}
//-------------------------------------------------------------
 
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out
while(in>0 && // until smaller one found,
 
a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//-------------------------------------------------------------
 
} // end class ArrayInOb
////////////////////////////////////////////////////////////////

main

class ObjectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayInOb arr; // reference to array
arr = new ArrayInOb(maxSize); // create the array
arr.insert("Evans", "Patty", 24, 9);
arr.insert("Smith", "Doc", 19, 4);
arr.insert("Smith", "Lorraine", 37, 6);
arr.insert("Smith", "Paul", 17, 11);
arr.insert("Yee", "Tom", 13, 7);
arr.insert("Hashimoto", "Sato", 21, 12);
arr.insert("Stimson", "Henry", 29, 2);
arr.insert("Velasquez", "Jose", 12, 1);
arr.insert("Vang", "Minh", 22, 3);
arr.insert("Creswell", "Lucinda", 18, 10);
System.out.println("Before sorting:");
arr.display(); // display items
arr.insertionSort(); // insertion-sort them
System.out.println("After sorting:");
arr.display(); // display them again
} // end main()
} // end class ObjectSortApp

So I'm creating like an array of objects... :sad: :sad: :sad:

So I have the option to sort them by their name, day or month?

So, same thing, vector of crap ?

Member Avatar for iamthwee

So, same thing, vector of crap ?

mmm, I guess, but can't you have a class which contains an array of objects?

Like that java code I posted. I dunno I'm so confused.

mmm, I guess, but can't you have a class which contains an array of objects?

Sure you can have an array. An array has fixed size. Do you mean an array of (class) objects, or do you really mean a class with an array inside?

Member Avatar for iamthwee

I think i mean this:-

an array of (class) objects

as that would appear to be more useful. - who knows i'm just guessing here, i'm probably getting confused as to what an object is. He he.

It's basically the same convenient structure as I have posted in that java code. Does that java code make sense to you guys?

The whole point is for me to try and understand classes better...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.