# Sort at TPS file

**URL:** <https://clarionhub.com/t/sort-at-tps-file/7078>\
**Category:** questions\
**Tags:** csv, tps\
**Created:** [April 4, 2024, 3:35pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078 "2024-04-04T15:35:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WesVec](https://clarionhub.com/user_avatar/clarionhub.com/wesvec/32/2256_2.png) [@WesVec](https://clarionhub.com/u/WesVec)\
**Post date:** [April 4, 2024, 3:36pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/1 "2024-04-04T15:36:00Z")

</div>

Is there a way to sort a tps file. I import data from a csv and would like to sort by state,city,name. I know this can be done in a queue but looking for a simple way.

---

<div class="post-metadata">

**Author:** ![Rick\_UpperPark](https://clarionhub.com/user_avatar/clarionhub.com/rick_upperpark/32/346_2.png) [@Rick\_UpperPark](https://clarionhub.com/u/Rick_UpperPark)\
**Post date:** [April 4, 2024, 3:48pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/2 "2024-04-04T15:48:36Z")

</div>

If you want to sort a TPS file by something you need to declare keys on the table definition.

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 4, 2024, 4:14pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/3 "2024-04-04T16:14:48Z")

</div>

The TopSpeed driver supports dynamic indexes. The BUILD statement allows to build dynamic indexes using the list of components and filter specified at run-time. See topics for INDEX and BUILD in documentation.

---

<div class="post-metadata">

**Author:** ![KevinErskine](https://clarionhub.com/user_avatar/clarionhub.com/kevinerskine/32/711_2.png) [@KevinErskine](https://clarionhub.com/u/KevinErskine)\
**Post date:** [April 4, 2024, 4:47pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/4 "2024-04-04T16:47:38Z")

</div>

Indexes not very inefficient (It has to build the index first) and this is not multi-user friendly (If another user chooses a different index it corrupts the 1st one)

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [April 4, 2024, 5:33pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/5 "2024-04-04T17:33:32Z")

</div>

A way to view the file in an Ad Hoc order is to setup a Browse but do not pick a Key. Then in the template pick Additional Sort Fields and specify them. I don’t think this allows a locator, at least not in Legacy. You can do the same in a Process or Report.

 ![image](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/a/a5a9c0562c2ceca83fb69b283fb2eedb25d5f3db.png)

These all use the VIEW engine and ORDER() or Prop:Order. You can hand code your own View with Order. Easy way to do that is set it up in a Process template to see the code. You must code the View code in a specific order i.e. Open / Set, etc.

 ![image](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/7/7b02cc6bb91b3556eb2630b8c66791c176954aa9.png)

In ABC the code is hidden inside the process class. Legacy generates more Clarion language code.

```auto
Process:View VIEW(Customers)
                     END

  ThisProcess.Init(Process:View, Relate:Customers, , )
  ThisProcess.AddSortOrder()
  ThisProcess.AppendOrder('+CUS:State,+CUS:City,+CUS:Company')
...

```

---

<div class="post-metadata">

**Author:** ![anon77170705](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@anon77170705](https://clarionhub.com/u/anon77170705)\
**Post date:** [April 4, 2024, 6:09pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/6 "2024-04-04T18:09:12Z")

</div>

> [@KevinErskine](#):
>
> this is not multi-user friendly

Concurrent work with the same ISAM file requires synchronization regardless uses of INDEXes.

If all required sort orders of file records are known, KEYs are enough. Otherwise, there are only 2 variants: either to use dynamic INDEXes or load entire table (or filtered records) to a queue.

---

<div class="post-metadata">

**Author:** ![WesVec](https://clarionhub.com/user_avatar/clarionhub.com/wesvec/32/2256_2.png) [@WesVec](https://clarionhub.com/u/WesVec)\
**Post date:** [April 4, 2024, 6:12pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/7 "2024-04-04T18:12:01Z")

</div>

Thanks everyone. I am using the legacy to pull out a subset of a larger tps file as follows.

```auto
  ! Start of "Legacy: Activity for each record"
  ! [Priority 4000]
  BRO:MCNUMBER=sub(Car:MCNUMBER,3,8)
  get(BROKER,BRO:BY_MCNUMBER)
  if ~error()
     mod:record=bro:record
     append(BroMode) 
  end

    ! Start of "Legacy: End of Procedure, After Closing Files"
    ! [Priority 4000]
    build(BroMode)

```

Works fine except the state,city,name is no longer sorted.

Checking to see if I can put Carl’s post into play. Have not had to work with this data in years.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [April 4, 2024, 8:37pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/8 "2024-04-04T20:37:00Z")

</div>

> [@WesVec](#):
>
> `append(BroMode)`  
> …

> [@WesVec](#):
>
> ```auto
> ! Start of "Legacy: End of Procedure, After Closing Files"
> ! [Priority 4000]
> build(BroMode)
> 
> ```

Check for Errors. The ` build(BroMode)` needs the file to be Open so probably getting error 37 file not open.

Rather than `APPEND()` (that does not update Keys) instead use `ADD()` and `STREAM / FLUSH` to make it faster.

* * *

I use the below Err4Msg() function to make code to display complete error info easy e.g.

```auto
   Build(BroMode)
   IF ERRORCODE() THEN 
      Message('Xyz failed Build(BroMode)' & Err4Msg(),'Build Error')
!or
      Stop('Xyz failed Build(BroMode)' & Err4Msg())
   END

```

```auto
Err4Msg FUNCTION (Byte FmtType=0)!,STRING 
  CODE 
    IF ~ERRORCODE() THEN RETURN ''.
    CASE FmtType
    OF 1 !the 1 line format for use by logging
        RETURN ERRORCODE()&' '&CLIP(ERROR()) & | ! {148}
            CHOOSE(~FILEERRORCODE(),'',' [Driver ' & CLIP(FILEERRORCODE())&' '&CLIP(FILEERROR()) &']' ) & | !driver error in [] !8/30 Carl was CHOOSE(ERRORCODE()<>90 but err 47 too, assume blank
            CHOOSE(~ERRORFILE(),'',' {{' & CLIP(ERRORFILE()) & '}' ) !Just throw a line break so the file is below

    END
    RETURN '<13,10><13,10>Error<160>Code:<160>' & ERRORCODE()&'<160>'&CLIP(ERROR()) & | ! {148}
            CHOOSE(~FILEERRORCODE(),'','<13,10>Driver<160>Error:<160>' & CLIP(FILEERRORCODE())&'<160>'&CLIP(FILEERROR()) ) & | !8/30 Carl was CHOOSE(ERRORCODE()<>90 but err 47 too, assume blank
            CHOOSE(~ERRORFILE(),'','<13,10>File<160>Name:<160>' & CLIP(ERRORFILE()) )

```

---

<div class="post-metadata">

**Author:** ![WesVec](https://clarionhub.com/user_avatar/clarionhub.com/wesvec/32/2256_2.png) [@WesVec](https://clarionhub.com/u/WesVec)\
**Post date:** [April 4, 2024, 9:59pm UTC](https://clarionhub.com/t/sort-at-tps-file/7078/9 "2024-04-04T21:59:14Z")

</div>

Thanks Carl, I really appreciate the help. Have stream/flush and moved the build to before closing. Everything seems correct. This is truck data from the Department of Transportation - They changed all the data reporting so been some work to put back together. This is a subset just the 358500 companies in interstate operations from the total of around 3 million. Thanks again.

Don Harvey

> **[Trucking Register - Home](https://www.truckingregister.com/)**
