Routing in mobile ad hoc networks is based on the cooperation of the network's nodes. The presence of selfish nodes that do not cooperate in this task drastically reduces the number of delivered packets. In order to find the better paths that include nodes willing to cooperate, we propose a new routing algorithm based on the reputation of the nodes. In our proposal, each node locally assigns a reputation value to the rest of the nodes in the network and next it uses the assigned reputation values to find out the better routing paths, in order to minimize the overall packet loss ratio. We assume that nodes have a stationary routing behavior, but we also include a mechanism to detect changes in their behavior. Our approach has been evaluated in the presence of selfish nodes, in order to compare it with the dynamic source routing algorithm, obtaining a reduction in the packet loss ratio at the expenses of a small increase in the number of hops taken by the packets to reach their destinations.