         @_selectedProject = n
         @selectedAccount  = @_data[n.url].account
         @queries          = @_data[n.url].queries
+        @_sortSelectedProjectTop(@_tickets)
         @fireEvent(@SELECTED_PROJECT_CHANGED, @, n)
         Log.debug("selectedProject set: " + n.text)
 

    ###
    addTicket: (ticket) ->
      @_tickets.add ticket
      @_tickets.set _sortTickets(@_tickets)
      @fireEvent(@TICKETS_CHANGED, @)

    ###*

    # sort tickets.
    #  order: account -> project -> ticket
    ###
    _sortTickets: (tickets) ->
      tickets.sort (a, b) ->
        if a.url > b.url then return  1
        if a.url < b.url then return -1
        if a.project.id > b.project.id then return  1
        if a.project.id < b.project.id then return -1
        if a.id > b.id then return  1
        if a.id < b.id then return -1
        return 0
      @_sortSelectedProjectTop(tickets)


    ###*
    # sort tickets by selectedProject.
    ###
    _sortSelectedProjectTop: (tickets) ->
      tickets.sort (a, b) =>
        isAselected = a.url is @selectedProject.url and a.project.id is @selectedProject.id
        isBselected = b.url is @selectedProject.url and b.project.id is @selectedProject.id
        if isAselected is isBselected then return 0
        if isAselected then return -1
        if isBselected then return  1

  return new DataAdapter

