void cListBox::SortByColumn(string ColumnName, bool Descending) { if (m_Items == 0) // any items in list? return; // Find the column id from the supplied column name int col_id = -1, col_ref = -1; if (ColumnName == "<[=-UNSORT_THIS_BITCH-=]>") { // we're un-sorting, so reference the extra "unsorted order" column col_id = LISTBOX_COLUMNS; col_ref = -1; } else { for (int j = 0; j<m_ColumnCount; j++) { if (m_ColumnName[j] == ColumnName) { col_id = m_ColumnSort[j]; col_ref = j; break; } } if (col_id == -1) // match found for column name? return; } // Sort the list m_Items = BubbleSortList(m_Items, m_NumElements, col_id, Descending); // Update m_LastItem and m_Position cListItem* current = m_Items; int count = 0; while (current) { if (current->m_Selected) { if (count < m_Position) m_Position = count; else if (count >(m_Position + m_NumDrawnElements - 1)) { while (count > (m_Position + m_NumDrawnElements)) m_Position++; m_Position++; } } if (current->m_Next == 0) { m_LastItem = current; break; } count++; current = current->m_Next; } if (m_ShowHeaders && col_ref > -1) { // Prepare Ascending/Descending image indicator for column header int dwidth; if (col_ref < m_ColumnCount - 1) dwidth = m_ColumnOffset[col_ref + 1] - 6 - m_ColumnOffset[col_ref]; else dwidth = m_eWidth - 19 - m_ColumnOffset[col_ref]; if (col_ref == 0) dwidth -= 2 + m_BorderSize; double stretch = (double)dwidth / (double)m_HeaderSortDesc->w; if (Descending) m_HeaderSortBack = zoomSurface(m_HeaderSortDesc, stretch, 1, 1); else m_HeaderSortBack = zoomSurface(m_HeaderSortAsc, stretch, 1, 1); } m_ScrollBar->SetTopValue(m_Position); }
void DoSort(FILE *fp) { /* Step1 a Open the file and generate linked list */ trnx *t; char line[BUFFER]; char *tokens; char *op,*timeval,*amt,*description; int errCheck = 1; /* Generate a list and Initialise it */ My402List list; memset(&list,0,sizeof(My402List)); (void)My402ListInit(&list); /* */ fgets(line,BUFFER,fp); while (!feof(fp)) { /* Perform the necessary error checks for the line */ errCheck = errorCheckLine(line); if (errCheck == 0) { exit(0); } /* Extract each element in the file and store it each attribute of transaction. */ t = malloc(sizeof(trnx)); tokens = malloc(strlen(line)); amt = malloc(strlen(line)); strcpy(tokens,line); op = strtok(tokens,"\t"); timeval = strtok(NULL,"\t"); amt = strtok(NULL,"\t"); description = strtok(NULL,"\t"); /* Error Check for each of the tokens in the line */ /* Operator */ errCheck = 1; errCheck = errCheckOp(t,op); if (errCheck == 0) { fputs("Operator is invalid.Must be +/-\n",stderr); exit(0); } /* Time */ errCheck = 1; errCheck = errCheckTime(&list,t,timeval); if (errCheck == 0) { fputs("Timestamp is invalid.\n",stderr); exit(0); } /* Amount */ errCheck = 1; errCheck = errCheckAmount(t,amt); if (errCheck == 0) { fputs("Amount is invalid.\n",stderr); exit(0); } /* Description */ errCheck = errCheckDesc(t,description); if (errCheck == 0) { exit(0); } /* Create node in the list */ (void)My402ListAppend(&list,(void *)t); /* Get next element in the list */ //free(tokens); //free(amt); fgets(line,BUFFER,fp); } fclose(fp); //PrintList(&list); BubbleSortList(&list,list.num_members); //PrintList(&list); DisplayOutput(&list); }