Results 1 to 8 of 8

Thread: Idea for ordered SWAP

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Idea for ordered SWAP

    It is common to use SWAP to put two items in ascending order, like this:

    DIM ONE, TWO AS LONG
    ' ...
    
    IF ONE > TWO THEN SWAP ONE, TWO
    
    This type of code is so common, it would be convenient to combine the two actions into one. Suppose we called this capability ORDER. Then, the code above could be shortened to,

    ORDER ONE, TWO
    
    It would not make sense for SWAP to take anything other than two operands, but for ORDER, it would. So, there is no reason why ORDER couldn't arrange the values of multiple variables in ascending order:

    ORDER ONE, TWO, THREE
    
    In the ORDER statement (or function) each variable would have to be of the same type.

    If ORDER were designed as a function rather than a statement, the returned value could be boolean, with TRUE being returned if any rearrangement of values occurred, otherwise FALSE.

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    Extended:
    String One, Two, Three
    One = "I don't care"
    Two = "1000"
    Three = MKL$(123)
    String myArray() = Order [One, Two, Three, "and also this"], Collate Ucase
    
    Last edited by ReneMiner; 15-07-2013 at 18:51.
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    After my initial post, it did occur to me that ORDER could/should be applicable to arrays, and that for strings, a case-insensitive version might be handly, and you noted. I am not quite sure how the syntax would work in practice, but it seems like a good idea to attempt it.

    It turns out that when people say "case insensitive", the expression is curiously backwards. When an operation like sorting is "case sensitive" it means that upper/lower case data is considered distinct, and therefore while handling characters, no tests for upper/lower case or case conversions of data are performed, but rather, the character encoding of the data values is used "as is". The very lack of 'action' with respect to case means that "case sensitive" processing is actually 'IN-sensitive' to the data's content. Conversely, "case insensitive" means that upper/lower case data is considered equivalent, but to obtain such an equivalence, a case-conversion function is required which must take into account the difference between upper and lower case, and so it must be 'sensitive' to those differences. As I say, it's backwards, but everyone seems to know what is meant ...

    If we really wanted special handling for strings, perhaps we need ORDER for numbers and ORDER$ for strings. If so, a "case insensitive" version could be made by translating the data to upper case for the compare phase of the swaps that are done. That would mean there might perhaps be a U_ORDER$ function. And, since there could be slight differences between how data is ordered if the comparisons are done in lower case (that is, if non-alpha data were interspersed) there could be an argument for an L_ORDER$ function too. Most people don't care much about that degree of subtlety though, and a U_ORDER$ function is probably enough.

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    ok, you got me- I just would like to have the multiple usable array-constructor
    ["treat", "or", "make", "this", "to", "be", "an", "array" ]
    
    as well as sub or function-parameter as also to be a constructor as this:
    String A() = ["Hello", "this", "is", "the", "first", "dimension"] _
                     ["and", "this", "is", "the", "second", "dimension"]
    
    which should result in a matrix as A(2,6)
    while this:
    String B() = ["This", "has", "only", "one", "dimension"]
    
    creates B(5)

    and perhaps some optional switches as this:
    String C() = ["One", "Two", "Three"], Sort Ascend| Descent, Collate Ucase
    
    Last edited by ReneMiner; 15-07-2013 at 20:25.
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    My goals for ORDER were less ambitious. There already exists an ARRAY SORT, so having ORDER apply to arrays is probably not necessary.

    However, I think there was a recent idea about using [ ] brackets for an "array constructor", as your prior comment suggests. That would be a separate thing, but I don't see why there's any reason you shouldn't be able to create an array with [ ] and then sort it with ARRAY SORT.

    That means you might be able create an array from individual items, then sort the array:

    DIM ONE, TWO, THREE, A() AS STRING
    ' ... set values for ONE, TWO and THREE
    A() = [ONE, TWO, THREE]
    ARRAY SORT A()
    
    or, if you wanted the item values sorted first, you could do it this way:

    DIM ONE, TWO, THREE, A() AS STRING
    ' ... set values for ONE, TWO and THREE
    ORDER ONE, TWO, THREE
    A() = [ONE, TWO, THREE]
    

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    yes- I edited already because I remembered sort-method. But my ambition is more about having passed parameters instantly ordered before a function uses them and it would be great to pass them in random order - while they could get proceeded sorted then. So maybe it's some totally different thing what you meant intentionally from that what my first idea of its use was. Especially I came upon the idea to use the brackets again because it could probably handle an arbitrary number of elements then.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Replies: 5
    Last Post: 30-07-2020, 10:53
  2. My Idea
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 4
    Last Post: 26-10-2012, 22:15
  3. New mad idea on UI module
    By ErosOlmi in forum thinBasic vaporware
    Replies: 17
    Last Post: 07-08-2008, 15:04
  4. Another idea
    By kryton9 in forum thinBasic General
    Replies: 2
    Last Post: 18-02-2008, 05:43

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •