Maps

  1. 4 years ago

    Joel_Eisenstat

    28 Aug 2019 User since 2016

    In adding a map element the order seems to change as elements are added. Is there an internal sort ?
    sample code;
    rs.moveFirst(); // walk list to obtain map values - time and row
    var i = 0;
    var mappy = [:];
    var x;
    while (i < rs.rowCount + 1) {
    x = rs.stringValue(1);// date incremented by unit ie 15 minutes
    mappy[i] = x;
    Console.write(mappy);
    rs.moveNext();
    i += 1;
    }
    Results attached
    TIA

  2. marco

    28 Aug 2019 Administrator User since 2016

    Maps are basically hash tables and hash tables keys are not sorted by design (for performance reason).
    Based on your code (since you are using a Number as the key) I think it would be really much better to use a List (that you can then sort):

    // loop to fill list with rs values
    rs.moveFirst();
    var list:List = [];
    while (rs.hasRow) {
        list.push(rs.stringValue(1));
        rs.moveNext();
    }
    
    // sort list in alphabetical order
    func compare (a, b) {
        return a > b;
    }
    list = list.sort(compare);
  3. Joel_Eisenstat

    29 Aug 2019 User since 2016

    Thanks I'll try that. Ultimately trying to highlight the list line that corresponds to the present time , calc the present time and match to the closest date between now and the unit increment. The order doesn't really matter as the map was used to find the line number of the schedule list to highlight.
    So the key used was a date which results in a runtime error ;

    var st = Date();//existing time
    var start = st.format("yyyy-MM-dd hh:mm:ss ZZZ", "TimeZone.system");
    var ed = st.dateByAddingMinutes(15);
    var finish = ed.format("yyyy-MM-dd hh:mm:ss ZZZ", "TimeZone.system");
    Label3.text = ed.format("yyyy-MM-dd hh:mm:ss ZZZ", "TimeZone.system");

    PostgreSQL1.connect
    var sql = 'SELECT * FROM"Calendar_Appointment" WHERE "resource_id" = 2 AND "date" >= $$\(start)$$ AND "date" <= $$\(finish)$$';
    var rs = PostgreSQL1.select(sql);
    Console.write(PostgreSQL1.errorMessage);
    TableView2.dataSet = rs;
    //match Date
    var d = rs.date;
    var dd = Date(d,"yyyy-MM-dd hh:mm:ss ZZZ", "TimeZone.system");
    Console.write(rs.date); // ok gives date/time now 2019-08-28 08:15:00

    var line = Window1.mappy[dd]; // runtime error
    //Runtime Error
    //Unable to perform operator LOADAT on object

    A list is generated with a single row - dd - but I can't see away to match it to the list in Table1 other than map

  4. Joel_Eisenstat

    30 Aug 2019 User since 2016

    Got it, switched to string comparison pulling out hh:mm , then used TableView1.scrollToRow(i, 2, true) when true. Using maps was over thinking the problem.

or Sign Up to reply!