How to sort the records of a text database file

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
alex298
Posts: 101
Joined: Mon May 01, 2006 1:17 pm

How to sort the records of a text database file

Post by alex298 » Sat Sep 04, 2010 2:23 am

Hello,

I have a .csv text database file with about 2,000 records. The content of the .csv text file is something like:
Peter,30,35000,Mary
David,28,68000,Single
...........................
...........................
...........................
I need to sort the Age (30, 28........) in Ascending and Descending order and put into a field. I am thinking and trying for a few hours. However it seems not an easy task.

Anybody has done similar task before? Could you please share your ideas or codes?

Thanks and best regards
Alex
Nice to meet all of you.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to sort the records of a text database file

Post by Klaus » Sat Sep 04, 2010 9:47 am

Hi Alex,

...
sort lines of your_var_with_csv_content numeric by item 2 of each
## sort lines of your_var_with_csv_content descending numeric by item 2 of each
...

should do the job :D


Best

Klaus

alex298
Posts: 101
Joined: Mon May 01, 2006 1:17 pm

Re: How to sort the records of a text database file

Post by alex298 » Sat Sep 04, 2010 10:38 am

Hi Klaus,

Thanks for your help.

I am consider using the Data Grid. It seems a better solution. However I have some problem in importing the .csv text file.

The data structure of the csv file is something like, for example:
Staff Number, Staff Address, Staff Age, Staff Salary
I knew how to import a single column:
put ("file:Files/staff_list.txt") into tempFile
put url tempFile into tFile

put empty into tDatei

## Link all daten
Repeat for each line i in tFile
put item 1 of i & CR after tDatei
End Repeat

put "Staff Number" & cr & tDatei into theText

## Let Data Grid know that first line has column names
put true into firstLineContainsColumnNames
set the dgText [ firstLineContainsColumnNames ] of group "DataGrid 1" to theText
I tried some times but still cannot figure out how to link all codes together so that all columns can be imported at the same time.

Please help!

Thanks and best regards
Alex
Nice to meet all of you.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to sort the records of a text database file

Post by Klaus » Sat Sep 04, 2010 11:05 am

Hi Alex,

just:

Code: Select all

...
## maybe sort your_var_with_csv_content first?
replace COMMA with TAB in your_var_with_csv_content
put "Staff Number" & TAB & "Staff Address" & TAB & "Staff Age" & TAB & "Staff Salary" & CR before your_var_with_csv_content
set the dgtext[TRUE]  of grp "your data grid here" to your_var_with_csv_content
## HINT! This (dgtext[true]) requires that all necessary columns have already been created!
...
Not tested, but should work :D


Best

Klaus

alex298
Posts: 101
Joined: Mon May 01, 2006 1:17 pm

Re: How to sort the records of a text database file

Post by alex298 » Mon Sep 06, 2010 11:12 am

Hi Klaus,

Thanks, it works perfectly :D

Best regards
Alex
Nice to meet all of you.

Post Reply